You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.2 KiB
53 lines
1.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Server
|
|
{
|
|
class PServerSocket
|
|
{
|
|
private readonly int BUFF_SIZE = 8192;
|
|
private Socket sock;
|
|
private IPEndPoint endPoint;
|
|
|
|
public PServerSocket(int port)
|
|
{
|
|
// create socket
|
|
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
endPoint = new IPEndPoint(IPAddress.Any, port);
|
|
}
|
|
|
|
public void Listen()
|
|
{
|
|
sock.Bind(endPoint);
|
|
|
|
sock.Listen(10);
|
|
|
|
Socket clientSocket = sock.Accept();
|
|
Console.WriteLine($"Socket Accepted ({clientSocket.RemoteEndPoint.ToString()})");
|
|
Console.WriteLine("Press any key to exit.");
|
|
|
|
byte[] buff = new byte[BUFF_SIZE];
|
|
while (!Console.KeyAvailable)
|
|
{
|
|
// receive socket
|
|
int n = clientSocket.Receive(buff);
|
|
|
|
string data = Encoding.UTF8.GetString(buff, 0, n);
|
|
Console.WriteLine($"Received: {data}");
|
|
|
|
// send socket
|
|
data = "Echo from server " + data;
|
|
buff = Encoding.UTF8.GetBytes(data);
|
|
clientSocket.Send(buff, 0, buff.Length, SocketFlags.None);
|
|
}
|
|
|
|
clientSocket.Close();
|
|
sock.Close();
|
|
}
|
|
}
|
|
}
|
|
|