473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Client auto exit when connect with server

13 New Member
i had been trying out the client server program. the client exit or crash by itself when connection establish with server. this error code 10045 being use inside the server code as the socket exception, but why the client still exit automatically?
Please help me, thanks you.

Expand|Select|Wrap|Line Numbers
  1. catch (ObjectDisposedException)
  2.             {
  3.                 System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
  4.             }
  5.             catch (SocketException se)
  6.             {
  7.                 if (se.ErrorCode == 10054) // Error code 
  8.                 {
  9.                     string msg = "User no." + socketData.m_clientNumber + " Disconnected" + "\n";
  10.                     AppendToRichEditControl(msg);
  11.                     m_workerSocketList[socketData.m_clientNumber - 1] = null;
  12.                     UpdateClientListControl();
  13.                 }
  14.                 else
  15.                 {
  16.                     MessageBox.Show(se.Message);
  17.                 }
  18.             }
Sep 24 '11 #1
2 2066
Aimee Bailey
197 Recognized Expert New Member
If you have a look at the message that comes along with the error code, you'll see it says "An existing connection was forcibly closed by the remote host", which means the server closed the connection. As the socket or TCP classes don't have events to tell you when a connection was dropped, this is usually the message you should handle to tell the user that the server disconnected unexpectedly.

Hope this helps.

Aimee.
Sep 28 '11 #2
haanjae
13 New Member
thx for reply me....
I try to debug again, i think this is the code that cause the Client program exit by itself, under public void WaitForData()

Expand|Select|Wrap|Line Numbers
  1. m_result = m_clientSocket.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, m_pfnCallBack, theSocPkt);
  2.  
but i dont know how to fixed it, because that code also exist in the server program. not sure which side is causing the error.
Please help, thanks a lot.

below is the full client code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Threading;
  12. namespace AsynClient
  13. {
  14.     public partial class Clientsoc : Form
  15.     {
  16.         byte[] m_dataBuffer = new byte[10];
  17.         IAsyncResult m_result;
  18.         public AsyncCallback m_pfnCallBack;
  19.  
  20.         public Socket m_clientSocket;
  21.         public Clientsoc()
  22.         {
  23.             InitializeComponent();
  24.  
  25.             txtbxipadd.Text = GetIP();
  26.         }
  27.  
  28.         void btnexit_Click(object sender, System.EventArgs e)
  29.         {
  30.             if (m_clientSocket != null)
  31.             {
  32.                 m_clientSocket.Close();
  33.                 m_clientSocket = null;
  34.             }
  35.             Close();
  36.         }
  37.  
  38.        void btnconnect_Click(object sender, System.EventArgs e)
  39.         {
  40.             // See if we have text on the IP and Port text fields
  41.             if (txtbxipadd.Text == "" || txtbxport.Text == "")
  42.             {
  43.                 MessageBox.Show("IP Address and Port Number are required to connect to the Server\n");
  44.                 return;
  45.             }
  46.             try
  47.             {
  48.                 UpdateControls(false);
  49.                 // Create the socket instance
  50.                 m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  51.  
  52.                 // Cet the remote IP address
  53.                 IPAddress ip = IPAddress.Parse(txtbxipadd.Text);
  54.                 int iPortNo = System.Convert.ToInt16(txtbxport.Text);
  55.                 // Create the end point 
  56.                 IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);
  57.                 // Connect to the remote host
  58.                 m_clientSocket.Connect(ipEnd);
  59.                 if (m_clientSocket.Connected)
  60.                 {
  61.                     UpdateControls(true);
  62.                     //Wait for data asynchronously 
  63.                     WaitForData();
  64.                 }
  65.             }
  66.             catch (Exception se)
  67.             {
  68.                 string str;
  69.                 str = "\nConnection failed, is the server running?\n" + se.Message;
  70.                 MessageBox.Show(str);
  71.                 UpdateControls(false);
  72.             }
  73.         }
  74.         void ButtonSendMessageClick(object sender, System.EventArgs e)
  75.         {
  76.             try
  77.             {
  78.                 string msg = richtxtbxmsg.Text;
  79.                 // New code to send strings
  80.                 NetworkStream networkStream = new NetworkStream(m_clientSocket);
  81.                 System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
  82.                 streamWriter.WriteLine(msg);
  83.                 streamWriter.Flush();
  84.  
  85.                 /* Use the following code to send bytes
  86.                 byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
  87.                 if(m_clientSocket != null){
  88.                     m_clientSocket.Send (byData);
  89.                 }
  90.                 */
  91.             }
  92.             catch (SocketException se)
  93.             {
  94.                 MessageBox.Show(se.Message);
  95.             }    
  96.         }
  97.         public void WaitForData()
  98.         {
  99.             try
  100.             {
  101.                 if (m_pfnCallBack == null)
  102.                 {
  103.                     m_pfnCallBack = new AsyncCallback(OnDataReceived);
  104.                 }
  105.                 SocketPacket theSocPkt = new SocketPacket();
  106.                 theSocPkt.thisSocket = m_clientSocket;
  107.                 // Start listening to the data asynchronously
  108.                m_result = m_clientSocket.BeginReceive(theSocPkt.dataBuffer, 0,theSocPkt.dataBuffer.Length, SocketFlags.None, m_pfnCallBack, theSocPkt); //causing exit
  109.  
  110.             }
  111.             catch (Exception se)
  112.             {
  113.                 MessageBox.Show(se.Message);
  114.             }
  115.         }
  116.  
  117.         public class SocketPacket
  118.         {
  119.             public System.Net.Sockets.Socket thisSocket;
  120.             public byte[] dataBuffer = new byte[1024];
  121.         }
  122.  
  123.         public void OnDataReceived(IAsyncResult asyn)
  124.         {
  125.             try
  126.             {
  127.                 SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
  128.                 int iRx = theSockId.thisSocket.EndReceive(asyn);
  129.                 char[] chars = new char[iRx + 1];
  130.                 System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
  131.                 int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
  132.                 System.String szData = new System.String(chars);
  133.                 richtxtbxstatuslog.Text = richtxtbxstatuslog.Text + szData;
  134.                 WaitForData();
  135.             }
  136.             catch (ObjectDisposedException)
  137.             {
  138.                 System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
  139.             }
  140.             catch (SocketException se)
  141.             {
  142.                 MessageBox.Show(se.Message);
  143.             }
  144.         }
  145.         private void UpdateControls(bool connected)
  146.         {
  147.             btnconnect.Enabled = !connected;
  148.             btndisconnect.Enabled = connected;
  149.             string connectStatus = connected ? "Connected" : "Not Connected";
  150.             richtxtbxstatuslog.Text = connectStatus;
  151.         }
  152.  
  153.         void btndisconnect_Click(object sender, EventArgs e)
  154.         {
  155.             if (m_clientSocket != null)
  156.             {
  157.                 m_clientSocket.Close();
  158.                 m_clientSocket = null;
  159.                 UpdateControls(false);
  160.             }
  161.         }
  162.         String GetIP()
  163.         {
  164.             String strHostName = Dns.GetHostName();
  165.  
  166.             // Find host by name
  167.             IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
  168.  
  169.             // Grab the first IP addresses
  170.             String IPStr = "";
  171.             foreach (IPAddress ipaddress in iphostentry.AddressList)
  172.             {
  173.                 IPStr = ipaddress.ToString();
  174.                 return IPStr;
  175.             }
  176.             return IPStr;
  177.         }
  178.         private void btnclear_Click(object sender, System.EventArgs e)
  179.         {
  180.             richtxtbxstatuslog.Clear();
  181.         }
  182.     }
  183. }
  184.  
Sep 29 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1573
by: Gaurav | last post by:
Hello, How can i connect client side javascript to sql server 2000 ? Thanks. Gaurav
1
1687
by: zelzel.zsu | last post by:
What is php's behavior when connect to a db? I've been told that php was once a dominant web programing language. I've read a few chapters of a php book. I was wonder what's php's performance....
0
1921
by: Asheesh | last post by:
Hi, I want to configure client machine with oracle database server thru .Net programming. Can any body tell me how? Actually the scenario is: I am crating .net setup project for complete...
6
6492
by: Valerian John | last post by:
I have a ListBox webcontrol on an aspx page. Items are added to the ListBox using client-side code. However, when the page is posted back the items are missing/not available. (It is like the...
4
1737
by: Emilio | last post by:
Question about Shared Sub Connect(server As , message As ) Why is in square brackets? Is it like Shared Sub Connect(server() As String, message() As String)
1
2181
by: techsatish | last post by:
hi all, I have a DB2 connect PE ver 8.2 installed in my windows client which tries to connect to AS/390 Database Instance via CCA.It uses TCP/IP. While trying to connect the Server from the...
1
2187
by: Steve | last post by:
Hi All I have a windows application (written in VB.net 2005) which I update via web downloads to my clients computers The downloaded file is saved in a subfolder until the next time the...
2
9765
by: jeffhan | last post by:
we have os/400 db2 database at the backend. i installed db2 v9 connect server on one windows server which is locating the same network with database server. now how to i configure the connect...
0
1505
by: 85ssp | last post by:
I am creating a small server client program that is meant for up to 70 connections from 70 different computers on a network. Everything in the program functions correctly except when testing...
6
10404
by: Jean-Marc Blaise | last post by:
Hi, I have installed DB2 9.5 directly from FP1 on Windows in custom mode with COMP=CONNECT_SUPPORT. I then added my DB2 Server license. I am surprise that db2licm refers to a DB2 Connect...
0
7202
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
7084
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
7328
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
7458
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
5013
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
3167
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
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.