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. -
catch (ObjectDisposedException)
-
{
-
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
-
}
-
catch (SocketException se)
-
{
-
if (se.ErrorCode == 10054) // Error code
-
{
-
string msg = "User no." + socketData.m_clientNumber + " Disconnected" + "\n";
-
AppendToRichEditControl(msg);
-
m_workerSocketList[socketData.m_clientNumber - 1] = null;
-
UpdateClientListControl();
-
}
-
else
-
{
-
MessageBox.Show(se.Message);
-
}
-
}
2 2066
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.
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() -
m_result = m_clientSocket.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, m_pfnCallBack, theSocPkt);
-
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: -
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.Net;
-
using System.Net.Sockets;
-
using System.Threading;
-
namespace AsynClient
-
{
-
public partial class Clientsoc : Form
-
{
-
byte[] m_dataBuffer = new byte[10];
-
IAsyncResult m_result;
-
public AsyncCallback m_pfnCallBack;
-
-
public Socket m_clientSocket;
-
public Clientsoc()
-
{
-
InitializeComponent();
-
-
txtbxipadd.Text = GetIP();
-
}
-
-
void btnexit_Click(object sender, System.EventArgs e)
-
{
-
if (m_clientSocket != null)
-
{
-
m_clientSocket.Close();
-
m_clientSocket = null;
-
}
-
Close();
-
}
-
-
void btnconnect_Click(object sender, System.EventArgs e)
-
{
-
// See if we have text on the IP and Port text fields
-
if (txtbxipadd.Text == "" || txtbxport.Text == "")
-
{
-
MessageBox.Show("IP Address and Port Number are required to connect to the Server\n");
-
return;
-
}
-
try
-
{
-
UpdateControls(false);
-
// Create the socket instance
-
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-
-
// Cet the remote IP address
-
IPAddress ip = IPAddress.Parse(txtbxipadd.Text);
-
int iPortNo = System.Convert.ToInt16(txtbxport.Text);
-
// Create the end point
-
IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);
-
// Connect to the remote host
-
m_clientSocket.Connect(ipEnd);
-
if (m_clientSocket.Connected)
-
{
-
UpdateControls(true);
-
//Wait for data asynchronously
-
WaitForData();
-
}
-
}
-
catch (Exception se)
-
{
-
string str;
-
str = "\nConnection failed, is the server running?\n" + se.Message;
-
MessageBox.Show(str);
-
UpdateControls(false);
-
}
-
}
-
void ButtonSendMessageClick(object sender, System.EventArgs e)
-
{
-
try
-
{
-
string msg = richtxtbxmsg.Text;
-
// New code to send strings
-
NetworkStream networkStream = new NetworkStream(m_clientSocket);
-
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
-
streamWriter.WriteLine(msg);
-
streamWriter.Flush();
-
-
/* Use the following code to send bytes
-
byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
-
if(m_clientSocket != null){
-
m_clientSocket.Send (byData);
-
}
-
*/
-
}
-
catch (SocketException se)
-
{
-
MessageBox.Show(se.Message);
-
}
-
}
-
public void WaitForData()
-
{
-
try
-
{
-
if (m_pfnCallBack == null)
-
{
-
m_pfnCallBack = new AsyncCallback(OnDataReceived);
-
}
-
SocketPacket theSocPkt = new SocketPacket();
-
theSocPkt.thisSocket = m_clientSocket;
-
// Start listening to the data asynchronously
-
m_result = m_clientSocket.BeginReceive(theSocPkt.dataBuffer, 0,theSocPkt.dataBuffer.Length, SocketFlags.None, m_pfnCallBack, theSocPkt); //causing exit
-
-
}
-
catch (Exception se)
-
{
-
MessageBox.Show(se.Message);
-
}
-
}
-
-
public class SocketPacket
-
{
-
public System.Net.Sockets.Socket thisSocket;
-
public byte[] dataBuffer = new byte[1024];
-
}
-
-
public void OnDataReceived(IAsyncResult asyn)
-
{
-
try
-
{
-
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
-
int iRx = theSockId.thisSocket.EndReceive(asyn);
-
char[] chars = new char[iRx + 1];
-
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
-
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
-
System.String szData = new System.String(chars);
-
richtxtbxstatuslog.Text = richtxtbxstatuslog.Text + szData;
-
WaitForData();
-
}
-
catch (ObjectDisposedException)
-
{
-
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
-
}
-
catch (SocketException se)
-
{
-
MessageBox.Show(se.Message);
-
}
-
}
-
private void UpdateControls(bool connected)
-
{
-
btnconnect.Enabled = !connected;
-
btndisconnect.Enabled = connected;
-
string connectStatus = connected ? "Connected" : "Not Connected";
-
richtxtbxstatuslog.Text = connectStatus;
-
}
-
-
void btndisconnect_Click(object sender, EventArgs e)
-
{
-
if (m_clientSocket != null)
-
{
-
m_clientSocket.Close();
-
m_clientSocket = null;
-
UpdateControls(false);
-
}
-
}
-
String GetIP()
-
{
-
String strHostName = Dns.GetHostName();
-
-
// Find host by name
-
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
-
-
// Grab the first IP addresses
-
String IPStr = "";
-
foreach (IPAddress ipaddress in iphostentry.AddressList)
-
{
-
IPStr = ipaddress.ToString();
-
return IPStr;
-
}
-
return IPStr;
-
}
-
private void btnclear_Click(object sender, System.EventArgs e)
-
{
-
richtxtbxstatuslog.Clear();
-
}
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Gaurav |
last post by:
Hello,
How can i connect client side javascript to sql server 2000 ?
Thanks.
Gaurav
|
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....
|
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...
|
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...
|
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)
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |