Socket Exception
-
I am trying to connect to a tcp listener, after I hit the
clint.Connect(server, port)
it throughts aSocketException
public void Connect(String server, String message)
{
try
{
StreamWriter sw;
StreamReader sr;int port = 5000; TcpClient client = new TcpClient(server, port); client.Connect(server, port); sw = new StreamWriter(client.GetStream()); sr = new StreamReader(client.GetStream()); sw.WriteLine(message); sw.Flush(); string rtnMsg = sr.ReadToEnd().Trim(); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } catch (IOException e) { } catch (NullReferenceException e) { } Console.WriteLine("\\n Press Enter to continue..."); Console.Read(); }
Any help would be really helpful.
-
I am trying to connect to a tcp listener, after I hit the
clint.Connect(server, port)
it throughts aSocketException
public void Connect(String server, String message)
{
try
{
StreamWriter sw;
StreamReader sr;int port = 5000; TcpClient client = new TcpClient(server, port); client.Connect(server, port); sw = new StreamWriter(client.GetStream()); sr = new StreamReader(client.GetStream()); sw.WriteLine(message); sw.Flush(); string rtnMsg = sr.ReadToEnd().Trim(); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } catch (IOException e) { } catch (NullReferenceException e) { } Console.WriteLine("\\n Press Enter to continue..."); Console.Read(); }
Any help would be really helpful.
The reason this happens is because you've already attempted to establish a connection in your constructor. The exception you're getting will be indicating that the port's already in use.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
The reason this happens is because you've already attempted to establish a connection in your constructor. The exception you're getting will be indicating that the port's already in use.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
It is just a method which is call by a thread, the method is passed the server IP to connect to. It throws an exception even when connecting only to one IP
-
It is just a method which is call by a thread, the method is passed the server IP to connect to. It throws an exception even when connecting only to one IP
Did you read what I said carefully? Basically, when you call
new TcpClient(address, port)
you are establishing a connection at that point. You can remove theConnect
call as you've already established your connection. See this[^] for more details.I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
I am trying to connect to a tcp listener, after I hit the
clint.Connect(server, port)
it throughts aSocketException
public void Connect(String server, String message)
{
try
{
StreamWriter sw;
StreamReader sr;int port = 5000; TcpClient client = new TcpClient(server, port); client.Connect(server, port); sw = new StreamWriter(client.GetStream()); sr = new StreamReader(client.GetStream()); sw.WriteLine(message); sw.Flush(); string rtnMsg = sr.ReadToEnd().Trim(); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } catch (IOException e) { } catch (NullReferenceException e) { } Console.WriteLine("\\n Press Enter to continue..."); Console.Read(); }
Any help would be really helpful.
Paul Harsent wrote:
TcpClient client = new TcpClient(server, port);
client.Connect(server, port);I'm with Pete here; you basically have provided the same information (server, port) twice; that would not make much sense, would it? The documentation[^] provides an example, it does not have a Connect() call. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Did you read what I said carefully? Basically, when you call
new TcpClient(address, port)
you are establishing a connection at that point. You can remove theConnect
call as you've already established your connection. See this[^] for more details.I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
Just let it go Pete... :)
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
Just let it go Pete... :)
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997I can't. I've got CDO.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
I can't. I've got CDO.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
I think you link should help him out. I went thru it and msdn is always helpful