using System.Net.Sockets;
// Connect to the server
TcpClient client = new TcpClient("server_address", 8080);
// Send a request to the server
NetworkStream stream = client.GetStream();
byte[] request = Encoding.UTF8.GetBytes("Hello from the client!");
stream.Write(request, 0, request.Length);
// Receive a response from the server
byte[] response = new byte[1024];
int bytesReceived = stream.Read(response, 0, response.Length);
String responseString = Encoding.UTF8.GetString(response, 0, bytesReceived);
Console.WriteLine("Response from server: " + responseString);
******************
using System.Net.Sockets;
// Set up the server
TcpListener server = new TcpListener(8080);
server.Start();
// Wait for a client to connect
TcpClient client = server.AcceptTcpClient();
// Read the request from the client
NetworkStream stream = client.GetStream();
byte[] request = new byte[1024];
int bytesReceived = stream.Read(request, 0, request.Length);
String requestString = Encoding.UTF8.GetString(request, 0, bytesReceived);
console.WriteLine("Request from client: " + requestString);
// Send a response to the client
byte[] response = Encoding.UTF8.GetBytes("Hello from the server!");
stream.Write(response, 0, response.Length);
No comments:
Post a Comment
Коментар: