Synchronized client socket example
The following example 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 the string to the server and then displays the string returned by the server in the console.
C#
using System;
using ;
using ;
using ;
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 = (())
IPAddress ipAddress = [0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
// Create a TCP/IP socket.
Socket sender = new Socket(,
, );
// Connect the socket to the remote endpoint. Catch any errors.
try {
(remoteEP);
("Socket connected to {0}",
());
// Encode the data string into a byte array.
byte[] msg = ("This is a test<EOF>");
// Send the data through the socket.
int bytesSent = (msg);
// Receive the response from the remote device.
int bytesRec = (bytes);
("Echoed test = {0}",
(bytes,0,bytesRec));
// Release the socket.
();
();
} catch (ArgumentNullException ane) {
("ArgumentNullException : {0}",());
} catch (SocketException se) {
("SocketException : {0}",());
} catch (Exception e) {
("Unexpected exception : {0}", ());
}
} catch (Exception e) {
( ());
}
}
public static int Main(String[] args) {
StartClient();
return 0;
}
}
Synchronous Server Socket Example The following example program creates a server that receives connection requests from the client. The server is generated using synchronous sockets.
Therefore, the execution of the server application is suspended while waiting for a connection from the client. The application receives strings from the client,
The string is displayed on the console and then echoes the string to the client. The string from the client must contain the string "<EOF>",
Send a signal indicating the end of the message.
C#
Copy the code
using System;
using ;
using ;
using ;
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.
// returns the name of the
// host running the application.
IPHostEntry ipHostInfo = (());
IPAddress ipAddress = [0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(,
, );
// Bind the socket to the local endpoint and
// listen for incoming connections.
try {
(localEndPoint);
(10);
// Start listening for connections.
while (true) {
("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket handler = ();
data = null;
// An incoming connection needs to be processed.
while (true) {
bytes = new byte[1024];
int bytesRec = (bytes);
data += (bytes,0,bytesRec);
if (("<EOF>") > -1) {
break;
}
}
// Show the data on the console.
( "Text received : {0}", data);
// Echo the data back to the client.
byte[] msg = (data);
(msg);
();
();
}
} catch (Exception e) {
(());
}
("\nPress ENTER to continue...");
();
}
public static int Main(String[] args) {
StartListening();
return 0;
}
}
The following example 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 the string to the server and then displays the string returned by the server in the console.
C#
using System;
using ;
using ;
using ;
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 = (())
IPAddress ipAddress = [0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
// Create a TCP/IP socket.
Socket sender = new Socket(,
, );
// Connect the socket to the remote endpoint. Catch any errors.
try {
(remoteEP);
("Socket connected to {0}",
());
// Encode the data string into a byte array.
byte[] msg = ("This is a test<EOF>");
// Send the data through the socket.
int bytesSent = (msg);
// Receive the response from the remote device.
int bytesRec = (bytes);
("Echoed test = {0}",
(bytes,0,bytesRec));
// Release the socket.
();
();
} catch (ArgumentNullException ane) {
("ArgumentNullException : {0}",());
} catch (SocketException se) {
("SocketException : {0}",());
} catch (Exception e) {
("Unexpected exception : {0}", ());
}
} catch (Exception e) {
( ());
}
}
public static int Main(String[] args) {
StartClient();
return 0;
}
}
Synchronous Server Socket Example The following example program creates a server that receives connection requests from the client. The server is generated using synchronous sockets.
Therefore, the execution of the server application is suspended while waiting for a connection from the client. The application receives strings from the client,
The string is displayed on the console and then echoes the string to the client. The string from the client must contain the string "<EOF>",
Send a signal indicating the end of the message.
C#
Copy the code
using System;
using ;
using ;
using ;
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.
// returns the name of the
// host running the application.
IPHostEntry ipHostInfo = (());
IPAddress ipAddress = [0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(,
, );
// Bind the socket to the local endpoint and
// listen for incoming connections.
try {
(localEndPoint);
(10);
// Start listening for connections.
while (true) {
("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket handler = ();
data = null;
// An incoming connection needs to be processed.
while (true) {
bytes = new byte[1024];
int bytesRec = (bytes);
data += (bytes,0,bytesRec);
if (("<EOF>") > -1) {
break;
}
}
// Show the data on the console.
( "Text received : {0}", data);
// Echo the data back to the client.
byte[] msg = (data);
(msg);
();
();
}
} catch (Exception e) {
(());
}
("\nPress ENTER to continue...");
();
}
public static int Main(String[] args) {
StartListening();
return 0;
}
}