using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace Server
{
class Program
{
static void Main(string[] args)
{
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.100"), 7777);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("■ Server Info:");
Console.WriteLine("IP Address : {0}", serverEndPoint.Address);
Console.WriteLine("Port Number: {0}", serverEndPoint.Port);
Console.WriteLine("AddressFamily : {0}", serverEndPoint.AddressFamily);
// Associates a Socket with a local endpoint.
server.Bind(serverEndPoint);
// Places a Socket in a listening state.
// Parameter: The maximum length of the pending connections queue.
server.Listen(5);
Console.WriteLine("Waiting for a client...");
// Creates a new Socket for a newly created connection.
Socket client = server.Accept();
IPEndPoint clientEndPoint = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Client connected: {0}", clientEndPoint.Address);
byte[] sendBuff = Encoding.UTF8.GetBytes("Connected to the server.");
// Sends the specified number of bytes of data to a connected Socket, using the specified SocketFlags.
client.Send(sendBuff, sendBuff.Length, SocketFlags.None);
byte[] recvBuff = new byte[256];
// Receives data from a bound Socket into a receive buffer.
// Return: The number of bytes received.
if (client.Receive(recvBuff) != 0)
{
Console.WriteLine("Message from a client: " + Encoding.UTF8.GetString(recvBuff));
} else
{
Console.WriteLine("No message.");
}
// Closes the Socket connection and releases all associated resources.
client.Close();
server.Close();
}
}
}