473,473 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

socket programming in C#

Hello,
I have a question on socket programming in C#.
I have this code and it doesn't work correctly.

It sends the message correctly but it does not get the message that
the server sends back ( the program bellow gets blocked ). I know for
sure
that the server sends back a message ( I'm using a traffic sniffer and
I can
see what's being sent and received ) .'
....
So what's wrong here ?

best regards
TcpClient client = new TcpClient(hostName, pacsPort);

try
{
NetworkStream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.Write(message);
string response = sr.ReadLine(); // program blocks
here
Console.Write(response);
sw.Close();
sr.Close();
s.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine("Error.");
}

Jul 25 '07 #1
5 1761
On Jul 25, 2:56 pm, sebastian.ha...@gmail.com wrote:
Hello,
I have a question on socket programming in C#.
I have this code and it doesn't work correctly.

It sends the message correctly but it does not get the message that
the server sends back ( the program bellow gets blocked ). I know for
sure
that the server sends back a message ( I'm using a traffic sniffer and
I can
see what's being sent and received ) .'
...
So what's wrong here ?
Does the server include a carriage return or line feed?

Jon

Jul 25 '07 #2
I don't know ... How could I check and what could I do about it ?

>
Does the server include a carriage return or line feed?

Jon

Jul 25 '07 #3
Hi,

<se*************@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
>I don't know ... How could I check and what could I do about it ?
The problem is that you are decorating the NetworkStream with a
StreamReader, ReadLine() blocks and keep reading until an
Environment.NewLine string is received, if it's never received it will be
blocked for ever.

Do this, read one byte at a time and dump it in a StringBuilder or some
similar structure. Also you must know if the source is sending UNICODE (2
bytes per char) or ASCII (1 byte per char).
Jul 25 '07 #4
On Wed, 25 Jul 2007 11:10:18 -0700, Ignacio Machin ( .NET/ C# MVP )
<machin TA <"laceupsolutions.com>"wrote:
Do this, read one byte at a time and dump it in a StringBuilder or some
similar structure. [...]
For testing purposes, this might be appropriate. Otherwise, no...don't do
this.

It is unclear from the original post exactly what the data is to look
like. If it _will_ include a CR/LF to terminate a line, then using
ReadLine() isn't all that bad. If the format of the data is unknown,
other than knowing for sure that it's some kind of character format, then
using the Read() method would be better. You will have to loop, as Read()
will return each time there's _some_ data that's been received.

This all assumes that the character encoding for the stream is the same on
both ends, of course, as Ignacio points out.

Pete
Jul 25 '07 #5
se*************@gmail.com wrote on 7/26/2007 1:46:47 AM
>Hello,
I have a question on socket programming in C#.
I have this code and it doesn't work correctly.
It sends the message correctly but it does not get the message that
the server sends back ( the program bellow gets blocked ). I know for
sure
that the server sends back a message ( I'm using a traffic sniffer and
I can
see what's being sent and received ) .'
....
So what's wrong here ?
best regards
TcpClient client = new TcpClient(hostName, pacsPort);
try
{
NetworkStream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.Write(message);
string response = sr.ReadLine(); // program blocks
here
Console.Write(response);
sw.Close();
sr.Close();
s.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine("Error.");
}
<hr>
You want to connect to the client before you attach the streamreader and -writer. Besides that it is good practice to set the encoding of the streamreader to ASCII.

Leon
Jul 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pyguy2 | last post by:
Issues of socket programming can be wierd, so I'm looking for some comments. In my python books I find exclusive use of socket.close(). From my other readings, I know about a "partial close...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
10
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
0
by: shonen | last post by:
I'm currently attempting to connect to a shoutcast server pull down the information from here and then I'll parse it. I got this working with the httplib, which was great, the problem is I want...
8
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In...
3
by: Stuart | last post by:
I am in the process of teaching myself socket programming. I am "playing around" with some simple echo server-client programs for m the book TCP/IP Sockets in C. The Server program is: ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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 ...

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.