473,322 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Socket programming question

Hello,
I have a question related to socket programming.
I have this code:

TcpClient client = new TcpClient(hostName, port);

try
{
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.Write(message);
string result = sr.ReadLine();
Console.Write(result);
sw.Close();
sr.Close();
s.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine("Error.");
}
And the thing is that it doesn't work ...

Jul 25 '07 #1
2 1109
You didn't mention ^how^ it didn't work. What were your expectations,
and how were they not met?

A few things to point out, you should really be using the using
statement for things that implement IDisposable (the Stream, StreamReader,
etc, etc).

Additionally, I believe the StreamReader performs some buffering. If
so, then it's possible that it is trying to read bytes that haven't been
sent on the stream yet.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<se*************@gmail.comwrote in message
news:11*********************@g4g2000hsf.googlegrou ps.com...
Hello,
I have a question related to socket programming.
I have this code:

TcpClient client = new TcpClient(hostName, port);

try
{
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.Write(message);
string result = sr.ReadLine();
Console.Write(result);
sw.Close();
sr.Close();
s.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine("Error.");
}
And the thing is that it doesn't work ...

Jul 25 '07 #2
Sorry ... it was an incomplete message.. I removed it and posted a new
message ....

On Jul 25, 6:53 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
You didn't mention ^how^ it didn't work. What were your expectations,
and how were they not met?

A few things to point out, you should really be using the using
statement for things that implement IDisposable (the Stream, StreamReader,
etc, etc).

Additionally, I believe the StreamReader performs some buffering. If
so, then it's possible that it is trying to read bytes that haven't been
sent on the stream yet.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<sebastian.ha...@gmail.comwrote in message

news:11*********************@g4g2000hsf.googlegrou ps.com...
Hello,
I have a question related to socket programming.
I have this code:
TcpClient client = new TcpClient(hostName, port);
try
{
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
sw.Write(message);
string result = sr.ReadLine();
Console.Write(result);
sw.Close();
sr.Close();
s.Close();
client.Close();
}
catch (Exception e)
{
Console.WriteLine("Error.");
}
And the thing is that it doesn't work ...

Jul 25 '07 #3

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

Similar topics

2
by: Jean-Philippe Guyon | last post by:
Hello, I am trying to compile a class that uses socket using the Visual C++ ..NET compiler. I get the following error: ------ Build started: Project: infCommon, Configuration: Release Win32...
2
by: dream machine | last post by:
Hi all , with BegeinReceive I can build async method of Socket Class that Receive the data from the Socket Client . My question is , if I have this code that create 3 Receive Async Call : ...
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...
5
by: Justin Creasy | last post by:
If this is the wrong group for this posting please let me know and I'll move it. I have an application that has an ArrayList of sockets to clients. For standard one-to-one messages my...
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...
14
by: =?Utf-8?B?TWlrZVo=?= | last post by:
I have a sync socket application. The client is blocked with Socket.Receive(...) in a thread, another thread calls Socket.Close(). This unblock the blocked thread. But the socket server is still...
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: ...
1
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear Sir/Mam, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP address and port number. Later I have made the socket...
4
by: danomano | last post by:
The code below works fine. As of now I just set the buffer length real big and all is good. However, I hate the impreciseness of that. So my question in general is how do I know what length to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.