473,471 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C# Sockets wait and listen

28 New Member
I am working a c# socket based client application, the problem I am facing is I cannot get the reply of my message from the remote machine. The 'read' function returns (immediately) unsuccessful (0) most of the times. How can I wait for some response? Assume that the server needs some time (about 1 minute) for processing my request, before replying.

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.     TcpClient tcpclnt = new TcpClient();
  4.     Console.WriteLine("Connecting.....");
  5.     tcpclnt.Connect(m_host, m_port); // ip and port of server
  6.     Console.WriteLine("Connected");
  7.     Console.Write("Enter the string to be transmitted : ");
  8.     String str = Console.ReadLine();
  9.     Stream stm = tcpclnt.GetStream();
  10.     ASCIIEncoding asen = new ASCIIEncoding();
  11.     byte[] ba = asen.GetBytes(str);
  12.     Console.WriteLine("Transmitting.....");
  13.     stm.Write(ba, 0, ba.Length);
  14.  
  15.     byte[] bb = new byte[100];
  16.     int k = 0, counter = 0;
  17.  
  18.     k = stm.Read(bb, 0, 100);
  19.  
  20.     Console.Write("Response from "+m_host+": ");
  21.     for (int i = 0; i < k; i++)
  22.         Console.Write(Convert.ToChar(bb[i]));
  23.  
  24.     //Console.ReadKey(true);
  25.     tcpclnt.Close();
  26.     result = 1;
  27. }
Mar 11 '09 #1
1 7240
Plater
7,872 Recognized Expert Expert
You could consider waiting for tp.Available > 0.
But you would need to remember to allow other processes/threads be active during it, and maybe make a timeout
Mar 11 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Nick Keighley | last post by:
I'm probably missing something rather obvious, but is there a known problem with getting a Python based socket program to communicate with a C based socket program? A simple echo server written in...
1
by: Olaf Meding | last post by:
How can I used WaitForMultipleObjects() with sockets? The code (shown below) respresents a very simple echo server using sockets. I need WaitForMultipleObjects() so I can wait for both a new...
4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
7
by: Michi Henning | last post by:
Hi, I'm using a non-blocking connect to connect to a server. Works fine -- the server gets and accepts the connection. However, once the connection is established, I cannot retrieve either the...
1
by: jm | last post by:
Easy probably, please read on. I know some of you have commented already about some of my socket question. I appreciate that. I have a Form1: static void Main() { Application.Run(new...
3
by: derSchweiz | last post by:
Hi, Experimenting with sockets, and I think I got it half working. I have the following code: Private Sub socket_receive(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
1
by: verge | last post by:
hello everyone! how's it going? like everyone in here im in need of some help and good friendship along the way...take a look at this: //MODIFIED SO IT DEALS WITH WINDOWS FTP USING ACTIVE...
5
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a...
2
by: iLL | last post by:
Okay, so I'm tying to get use to programming with sockets, and I have run into a bit of a snag. As of now, my program basically works like this: Server: Listen for a connection Client:...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.