Ryan Walsh

Ryan Walsh

  • NA
  • 1
  • 5.5k

Need help C# WinSock TCP data recieve server!

Aug 13 2010 6:27 PM
Hey everyone,
I am developing code to accept TCP communcations from one computer to another. Basically from this forum, all I need is the ability to send a data string to the other computer. here is the code I have to do so. they both are the same application, but can be set to server mode or client mode depending on which computer. you will see the differences in the "radiobutton" events below. basically the server is set to listen.
 
  1. public MainForm()  
  2. {  
  3.     InitializeComponent();  
  4.       
  5.     tcpServer.LocalPort = 88;  
  6.     tcpServer.DataArrival += new AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEventHandler(tcpServer_DataArrival);             
  7.     tcpServer.ConnectionRequest += new AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEventHandler(tcpServer_ConnectionRequest);  
  8.     tcpServer.ConnectEvent +=new EventHandler(tcpServer_ConnectEvent);  
  9.     tcpServer.Error +=new AxMSWinsockLib.DMSWinsockControlEvents_ErrorEventHandler(tcpServer_Error);  
  10.     tcpServer.Close();  
  11.     this.FormClosing +=new FormClosingEventHandler(MainForm_FormClosing);  
  12.      
  13. }  
  14. private void tcpServer_Error(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent e)  
  15. {  
  16.     MessageBox.Show(e.description);  
  17. }  
  18. //added to close socket before program exit. this also is not working  
  19. //as subsequent calls to program returns "Address in use" error  
  20. private void MainForm_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)  
  21. {  
  22.     tcpServer.Close();  
  23. }  
  24. private void tcpServer_ConnectEvent(object sender, EventArgs e)  
  25. {  
  26.     richTextBox1.AppendText("TCP Connection Made...\r\n");  
  27.     try  
  28.     {  
  29.         tcpServer.SendData("Hey Ryan Here I am!");  //server does not receive this for some reason.  
  30.     }  
  31.     catch (Exception ex)  
  32.     {  
  33.         MessageBox.Show(ex.Message);  
  34.     }  
  35.        // tcpServer.Close();  
  36.       
  37. }  
  38.   
  39. private void tcpServer_ConnectionRequest(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)  
  40. {  
  41.     richTextBox1.AppendText("TCP Connection Requested...\r\n");              
  42.     tcpServer.Close();  
  43.       
  44.     tcpServer.Accept(e.requestID);  
  45.     isconnected = true;  
  46.     tcpServer.SendData("1"); //client side will receive this message for some reason  
  47. }  
  48. private void tcpServer_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)  
  49. {  
  50.     richTextBox1.AppendText("Receiving TCP Data...\r\n");  
  51.     string inputstr="";  
  52.     object data=(object)inputstr;  
  53.     tcpServer.GetData(ref data);  
  54.     richTextBox1.AppendText((string)data + "\r\n");  
  55.     if (radioServer.Checked)  
  56.     {  
  57.         inputstr = (string)data;  
  58.         ConnectModems();  
  59.         sendingData = inputstr;  
  60.     }  
  61. }  
  62. private void radioClient_CheckedChanged(object sender, EventArgs e)  
  63. {  
  64.     if (radioClient.Checked)  
  65.     {  
  66.         groupContacts.Visible = false;  
  67.         groupTests.Visible = false;  
  68.         tcpServer.Close();  
  69.         lblRole.Text = "CLIENT APP";  
  70.     }  
  71. }  
  72. private void radioServer_CheckedChanged(object sender, EventArgs e)  
  73. {  
  74.     if (radioServer.Checked)  
  75.     {  
  76.         tcpServer.Close();  
  77.         groupContacts.Visible = true;  
  78.         groupTests.Visible = true;  
  79.         tcpServer.Listen();  
  80.         lblRole.Text = "SERVER APP";  
  81.     }  
  82. }  

I have made the tcp connection, but calls to senddata() do not go through. please let me know what I am doing wrong! thanks!

Answers (1)