Synchronous Client Socket Sample
The following sample program creates a client that connects to the server. The client is generated with a synchronous socket, so the execution of the client application is suspended until the server returns a response. The application sends a string to the server, and then displays the string returned by the server in the console.
C#
Using System;
Using System.Net;
Using System.Net.Sockets;
Using System.Text;
public class Synchronoussocketclient {
public static void Startclient () {
Data buffer for incoming data.
byte[] bytes = new byte[1024];
Connect to a remote device.
try {
Establish the remote endpoint for the socket.
This example uses port 11000 on the local computer.
Iphostentry iphostinfo = Dns.resolve (Dns.gethostname ())
IPAddress IPAddress = iphostinfo.addresslist[0];
IPEndPoint remoteep = new IPEndPoint (ipaddress,11000);
Create a TCP/IP socket.
Socket sender = new socket (AddressFamily.InterNetwork,
SocketType.Stream, PROTOCOLTYPE.TCP);
Connect the socket to the remote endpoint. Catch any errors.
try {
Sender. Connect (remoteEP);
Console.WriteLine ("Socket connected to {0}",
Sender. Remoteendpoint.tostring ());
Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes ("This is a test<eof>");
Send the data through the socket.
int bytessent = sender. Send (msg);
Receive the response from the remote device.
int Bytesrec = sender. Receive (bytes);
Console.WriteLine ("echoed Test = {0}",
Encoding.ASCII.GetString (Bytes,0,bytesrec));
Release the socket.
Sender. Shutdown (Socketshutdown.both);
Sender. Close ();
} catch (ArgumentNullException ane) {
Console.WriteLine ("ArgumentNullException: {0}", ane. ToString ());
} catch (SocketException se) {
Console.WriteLine ("SocketException: {0}", se. ToString ());
} catch (Exception e) {
Console.WriteLine ("Unexpected exception: {0}", e.tostring ());
}
} catch (Exception e) {
Console.WriteLine (E.tostring ());
}
}
public static int Main (string[] args) {
Startclient ();
return 0;
}
}
Synchronous server Socket Sample The following sample program creates a server that receives a connection request from a client. The server is generated with a synchronous socket,
Therefore, the execution of the server application is suspended while waiting for a connection from the client. The application receives a string from the client,
Displays the string in the console, and then echoes the string back to the client. The string from the client must contain the string "<EOF>",
To emit a signal that indicates the end of the message.
C#
Copy Code
Using System;
Using System.Net;
Using System.Net.Sockets;
Using System.Text;
public class Synchronoussocketlistener {
Incoming data from the client.
public static string data = NULL;
public static void Startlistening () {
Data buffer for incoming data.
byte[] bytes = new byte[1024];
Establish the local endpoint for the socket.
Dns.gethostname returns the name of the
Host running the application.
Iphostentry iphostinfo = Dns.resolve (Dns.gethostname ());
IPAddress IPAddress = iphostinfo.addresslist[0];
IPEndPoint localendpoint = new IPEndPoint (ipAddress, 11000);
Create a TCP/IP socket.
Socket listener = new socket (AddressFamily.InterNetwork,
SocketType.Stream, PROTOCOLTYPE.TCP);
Bind the socket to the local endpoint and
Listen for incoming connections.
try {
Listener. Bind (Localendpoint);
Listener. Listen (10);
Start listening for connections.
while (true) {
Console.WriteLine ("Waiting for a Connection ...");
Program is suspended and waiting for an incoming connection.
Socket handler = listener. Accept ();
data = null;
An incoming connection needs to be processed.
while (true) {
bytes = new byte[1024];
int Bytesrec = handler. Receive (bytes);
Data + = Encoding.ASCII.GetString (Bytes,0,bytesrec);
if (data. IndexOf ("<EOF>") >-1) {
Break
}
}
Show the data on the console.
Console.WriteLine ("Text Received: {0}", data);
Echo the data back to the client.
byte[] msg = Encoding.ASCII.GetBytes (data);
Handler. Send (msg);
Handler. Shutdown (Socketshutdown.both);
Handler. Close ();
}
} catch (Exception e) {
Console.WriteLine (E.tostring ());
}
Console.WriteLine ("\npress ENTER to continue ...");
Console.read ();
}
public static int Main (string[] args) {
Startlistening ();
return 0;
}
}
The above is the C # (socket) Synchronous socket code sample content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!