Sockets in C#
-
Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project. The client is now able to communicate with 1 server at one ip and port.But i want the client to call another udp server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc) How i can do this?Please give me your suggestions
-
Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project. The client is now able to communicate with 1 server at one ip and port.But i want the client to call another udp server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc) How i can do this?Please give me your suggestions
Hi, what could be the problem? duplicate what you already have. And if there are any blocking calls, consider using an extra thread/backgroundworker. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project. The client is now able to communicate with 1 server at one ip and port.But i want the client to call another udp server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc) How i can do this?Please give me your suggestions
"Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project." - Most likely because you are using UDP and not TCP (if thats what was used before) UDP doesnt care about "connections" etc, it just sends/recieves to/from endpoints. Not because you moved the server part into a seperate project... "The client is now able to communicate with 1 server at one ip and port.But i want the client to call another UDP server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)" - You will have to define another endpoint for the UDP sending socket (client) with a different port, tis all. If you still are having problems, please post up some code; it might help clear things up.
modified on Saturday, March 14, 2009 1:30 PM
-
"Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project." - Most likely because you are using UDP and not TCP (if thats what was used before) UDP doesnt care about "connections" etc, it just sends/recieves to/from endpoints. Not because you moved the server part into a seperate project... "The client is now able to communicate with 1 server at one ip and port.But i want the client to call another UDP server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)" - You will have to define another endpoint for the UDP sending socket (client) with a different port, tis all. If you still are having problems, please post up some code; it might help clear things up.
modified on Saturday, March 14, 2009 1:30 PM
Hi, If iam defining a new endpoint the client will be able to call yet another udp server.But i want the client to recursively call the servers at different port for every 15 secs. //client
private void SendMessage()
{try { listBox6.Items.Add("Connecting...."); IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1);
listBox6.Items.Add("Connected");byte\[\] data = new byte\[1024\]; String snd = "hello"; data = Encoding.ASCII.GetBytes(snd); listBox6.Items.Add("Transmitting..."); sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep); listBox6.Items.Add("Sent..."); EndPoint tmpRemote = (EndPoint)ipep; listBox6.Items.Add("Message received from {0}:"); listBox6.Items.Add(ipep.ToString()); data = new byte\[1024\]; int recv = sock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote); String zz=Encoding.ASCII.GetString(data, 0, recv); listBox6.Items.Add(zz); if (zz == "Welcome to the Server") { lb9.Text = "Active"; lb9.BackColor = Color.Green; } else{ lb9.Text = "Inactive"; lb9.BackColor=Color.Red; } } catch (SocketException e) { //Console.WriteLine("Error..... " + e.StackTrace); MessageBox.Show(e.Message); } }
//server
public static void start_server()
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newsock.Bind(ipep); Console.WriteLine("Waiting for a client..."); while (true) { try { //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
-
"Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project." - Most likely because you are using UDP and not TCP (if thats what was used before) UDP doesnt care about "connections" etc, it just sends/recieves to/from endpoints. Not because you moved the server part into a seperate project... "The client is now able to communicate with 1 server at one ip and port.But i want the client to call another UDP server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)" - You will have to define another endpoint for the UDP sending socket (client) with a different port, tis all. If you still are having problems, please post up some code; it might help clear things up.
modified on Saturday, March 14, 2009 1:30 PM
Hi, I used multithreading concept so the client is able to communicate with any number of servers.Using the same concept i tried to call the 2nd server after 10 secs.When it comes to the client the 2nd time it gives"Invalid OperationException Cross-thread operation not valid: Control 'listBox6' accessed from a thread other than the thread it was created on." //server
public UdpServer()
{
try
{
Thread startServer = new Thread(new ThreadStart(start_server));
startServer.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
// onReceive.Start();try { Thread.Sleep(10000);
startServer2 = new Thread(new hreadStart(start_server2));
startServer2.Start(); } catch (Exception e) { Console.WriteLine(e.Message); } }
public static void start_server()
{IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newsock.Bind(ipep); Console.WriteLine("Waiting for a client..."); while (true) { try { //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050); EndPoint tmpRemote = (EndPoint)ipep; // EndPoint tmpRemote = (sender); byte\[\] data = new byte\[1024\]; Console.WriteLine("hai"); int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote); Console.WriteLine("gfgjfk"); Console.WriteLine("Message received from {0}:", tmpRemote.ToString()); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received data = new byte\[1024\]; string ss = "Welcome to the Server"; data = Encoding.ASCII.GetBytes(ss); newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote); Console.WriteLine("\\nSent Acknowledgement");
-
Hi, If iam defining a new endpoint the client will be able to call yet another udp server.But i want the client to recursively call the servers at different port for every 15 secs. //client
private void SendMessage()
{try { listBox6.Items.Add("Connecting...."); IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1);
listBox6.Items.Add("Connected");byte\[\] data = new byte\[1024\]; String snd = "hello"; data = Encoding.ASCII.GetBytes(snd); listBox6.Items.Add("Transmitting..."); sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep); listBox6.Items.Add("Sent..."); EndPoint tmpRemote = (EndPoint)ipep; listBox6.Items.Add("Message received from {0}:"); listBox6.Items.Add(ipep.ToString()); data = new byte\[1024\]; int recv = sock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote); String zz=Encoding.ASCII.GetString(data, 0, recv); listBox6.Items.Add(zz); if (zz == "Welcome to the Server") { lb9.Text = "Active"; lb9.BackColor = Color.Green; } else{ lb9.Text = "Inactive"; lb9.BackColor=Color.Red; } } catch (SocketException e) { //Console.WriteLine("Error..... " + e.StackTrace); MessageBox.Show(e.Message); } }
//server
public static void start_server()
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newsock.Bind(ipep); Console.WriteLine("Waiting for a client..."); while (true) { try { //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
-
"Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project." - Most likely because you are using UDP and not TCP (if thats what was used before) UDP doesnt care about "connections" etc, it just sends/recieves to/from endpoints. Not because you moved the server part into a seperate project... "The client is now able to communicate with 1 server at one ip and port.But i want the client to call another UDP server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)" - You will have to define another endpoint for the UDP sending socket (client) with a different port, tis all. If you still are having problems, please post up some code; it might help clear things up.
modified on Saturday, March 14, 2009 1:30 PM
Hi The following code gives me the output: Waiting for client... hai Waiting for client2..... hai Message received from... hello Sent ackn.... hai Message received from... server Sent ackn.... I want it to be like: Waiting for client... hai Message received from... hello Sent ackn.... Waiting for client 2... I tried to change but im getting"the existing connection was forcibly closed by the remote host"..Please help me with this //client
public void SendMessage()
{try { listBox6.Items.Add("Connecting...."); IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001); //IPEndPoint ipep = new IPEndPoint(); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1); //sock.Bind(ipep); listBox6.Items.Add("Connected"); byte\[\] data = new byte\[1024\]; String snd = "hello"; data = Encoding.ASCII.GetBytes(snd); listBox6.Items.Add("Transmitting..."); sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep);//sent hello //sock.SendTo(data, ipep);//sent hello listBox6.Items.Add("Sent..."); EndPoint tmpRemote = (EndPoint)ipep; listBox6.Items.Add("Message received from {0}:"); listBox6.Items.Add(ipep.ToString()); data = new byte\[1024\]; int recv = sock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote); String zz=Encoding.ASCII.GetString(data, 0, recv); listBox6.Items.Add(zz); if (zz == "Welcome to the Server") { lb9.Text = "Active"; lb9.BackColor = Color.Green; } else{ lb9.Text = "Inactive"; lb9.BackColor=Color.Red; } } catch (SocketException e) { //Console.WriteLine("Error..... " + e.StackTrace); MessageBox.Show(e.Message); } //SendMessage1(); } public void SendMessage1() { //same as above
-
"Hi, I have the client and Udp server communicate with each other.Now im not getting the exception "The existing connection was forcibly closed by the remote host".This is because i have defined the server in a separate project." - Most likely because you are using UDP and not TCP (if thats what was used before) UDP doesnt care about "connections" etc, it just sends/recieves to/from endpoints. Not because you moved the server part into a seperate project... "The client is now able to communicate with 1 server at one ip and port.But i want the client to call another UDP server at another port but same ip.For eg:the client is communicating with udp server at port 10001.After every 15 secs the client should connect with the servers at different ports(10002,10003 etc)" - You will have to define another endpoint for the UDP sending socket (client) with a different port, tis all. If you still are having problems, please post up some code; it might help clear things up.
modified on Saturday, March 14, 2009 1:30 PM
Hi I used the Thread.Sleep()
public UdpServer()
{
try
{
startServer = new Thread(new ThreadStart(start_server));
startServer.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
//startServer.Abort();
}try { Thread.Sleep(20000); startServer2 = new Thread(new ThreadStart(start\_server2)); startServer2.Start(); } catch (Exception e) { Console.WriteLine(e.Message); //startServer2.Abort(); } }
Im getting the output as Waiting for client... hai Message received.... hello Sent ack Waiting for client2... hai At this it shows"existing connection was forcibly closed by the remote host".Please give me your suggestion