Program | client
I am a novice socket, recently made a socket client program, connection server, if the server exists, and allow the connection, the program is correct, normal execution; however, if the server does not exist, or the connection is denied, the program will be stuck, without prompting for an error. At first I thought there was no catch exception, but checked the program, the exception was catch off, the program or card.
Please help me to correct the shrimp! Thank you, the following is my code for this module!
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Net;
Using System.Net.Sockets;
Using System.Threading;
Using System.Text;
Namespace test Program
{
<summary>
Summary description of the classclient.
</summary>
public class Classclient
{
Method
Public Classclient ()
{
//
TODO: Add constructor logic here
//
}
Function
#region Socket Communication Machine Connection function
<summary>
Socket Communication Machine Connection function
</summary>
<param name= "remoteEP" > Remote terminal </param>
<param name= "Client" > Establish clients </param>
Public byte Socketconnect (EndPoint remoteep, Socket Client)
{
Calling System connection functions
Client.beginconnect (Remoteep,new AsyncCallback (Connectcallback), Client);
Connectdone.waitone ();
return (1);
}
#endregion
#region Socket connection return function
<summary>
Socket connection return function
</summary>
<param name= "Ar" > indicates the status of the asynchronous Operation </param>
private static void Connectcallback (IAsyncResult ar)
{
Try
{
Get socket Connection Instance
Socket client = (socket) ar. asyncstate;
Completes the connection process.
Client. EndConnect (AR);
Position Connection Completion Flag
Connectdone.set ();
Get Connection Success Information
Connectinfo= "Connection Successful! ";
}
catch (Exception e)
{
Get Connection Failure Information
Connectinfo=e.tostring ();
End Connection
Connectdone.reset ();
}
}
#endregion
Closed function of #region socket communication organ
<summary>
Closed function of socket communication organ
</summary>
<param name= "Client" > Socket communication instance that needs to be closed </param>
Public byte socketclose (Socket Client)
{
Try
{
if (client!=null)
{
If communication information is still generated, disable
if (client.connected)
{
Client.shutdown (Socketshutdown.both);
}
Turn off socket traffic
Client.close ();
Get Shutdown Success Information
Closeinfo = "Communication machine is closed!";
}
return (1);
}
catch (Exception e)
{
Get Shutdown communication Failure information
Closeinfo = E.tostring ();
return (0);
}
}
#endregion
#region Data send function
///<summary>
///data send function
///< /summary>
///<param name= "Client" > Connected socket Communication Machine Instance (client) </param>
/// <param name= "Messagesend" > Information to be sent </param>
///<returns>
/// Returns whether the send Success value
///</returns>
public bool Socketsend (Socket Client, String Messagesend)
{
//converts data to byte ASCII code.
byte[] bytedata = Encoding.ASCII.GetBytes (messagesend);
Sends data to a remote device (Server).
Client.beginsend (bytedata, 0, Bytedata.length, socketflags.none,new AsyncCallback (Sendcallback), Client);
Send Flag Event wait
Senddone.waitone ();
return True
return (true);
}
#endregion
#region Data Send return function
<summary>
Data Send return function
</summary>
<param name= "Ar" > indicates the status of the asynchronous Operation </param>
private static void Sendcallback (IAsyncResult ar)
{
Try
{
Get socket Connection Instance
Socket Client = (socket) ar. asyncstate;
Send data to remote device complete
int bytessent = Client.endsend (AR);
Position Send completion flag
Senddone.set ();
Get Send completion information
sendinfo= "Send is done!" ";
}
catch (Exception e)
{
Get Send Failure message
Sendinfo=e.tostring ();
End Send Flag
Senddone.reset ();
}
}
#endregion
#region Data receive function
<summary>
Data receive function
</summary>
<param name= "Client" > Connected socket Communication Machine Instance (client) </param>
<returns>
Return receive data
</returns>
public string socketreceive (Socket Client)
{
Try
{
int i;
Create an instance.
StateObject state = new StateObject ();
State.worksocket = Client;
Start receiving data from a remote device
Client.beginreceive (state.buffer, 0, Stateobject.buffersize, 0,new AsyncCallback (ReceiveCallback), state);
Converts the received byte data to a string
For (I=0;i<state.buffer. Length; i++)
{
Revdata + = State.buffer[i]. ToString ();
}
Returns the data received
return (Revdata);
}
catch (Exception e)
{
Get Receive failure information
Revinfo = E.tostring ();
Returns an empty string
Return ("");
}
}
#endregion
#region Data receive return function
<summary>
Data Receive return function
</summary>
<param name= "Ar" > indicates the status of the asynchronous Operation </param>
private static void ReceiveCallback (IAsyncResult ar)
{
Try
{
Create an instance
StateObject state = (stateobject) ar. asyncstate;
Socket Client = State.worksocket;
Reading data from a remote device
int bytesread = client.endreceive (AR);
if (Bytesread > 0)
{
There may be too much data to store the string in the buffer first
revtempstring = Encoding.ASCII.GetString (State.buffer,0,bytesread);
State.SB.Append (revtempstring);
Receive remaining data
Client.beginreceive (State.buffer,0,stateobject.buffersize,0,new AsyncCallback (ReceiveCallback), state);
}
Else
{
All data has been received
if (State.SB.Length > 1)
{
String response = State.SB.ToString ();
}
Position data received flag bit
Receivedone.set ();
}
}
catch (Exception e)
{
Get Receive failure information
Revinfo=e.tostring ();
}
}
#endregion
#region Field
private static ManualResetEvent Senddone = new ManualResetEvent (false);
private static ManualResetEvent Connectdone = new ManualResetEvent (false);
private static ManualResetEvent Receivedone = new ManualResetEvent (false);
public static string connectinfo = "";
public static string closeinfo = "";
public static string sendinfo = "";
public static string revinfo = "";
public static string revdata = "";
public static string revtempstring = "";
public class StateObject
{
Client socket.
Public Socket worksocket = null;
Size of receive buffer.
public const int buffersize = 2048;
Receive buffer.
Public byte[] Buffer = new Byte[buffersize];
Received data string.
Public StringBuilder SB = new StringBuilder ();
}
#endregion
}
}