0% found this document useful (0 votes)
55 views

Socket Programming

The document contains code for a client-server application in C# that allows sending and receiving of messages between a client and server. The client code connects to the server, allows the user to enter a message, sends it to the server, and receives a reply back. The server code listens for connections, accepts connections from clients, and handles each client connection in a separate thread. It receives messages from clients and sends a hardcoded reply back.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Socket Programming

The document contains code for a client-server application in C# that allows sending and receiving of messages between a client and server. The client code connects to the server, allows the user to enter a message, sends it to the server, and receives a reply back. The server code listens for connections, accepts connections from clients, and handles each client connection in a separate thread. It receives messages from clients and sends a hardcoded reply back.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MyClient
{
class client
{
static void Main(string[] args)
{
Console.WriteLine("This is the client");

Socket master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);

master.Connect(ipEnd)
string sendData = "";
do
{
Console.WriteLine("Data to send: ");
sendData = Console.ReadLine();
master.Send(Encoding.UTF8.GetBytes(sendData));

//getting the reply


byte[] rdt = new byte[4];
master.Receive(rdt);
Console.WriteLine("our reply is: " + Encoding.UTF8.GetString(rdt));
} while (sendData.Length>0);
master.Close();
//Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyServer
{
class Server
{
static void Main(string[] args)
{
Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);

listenerSocket.Bind(ipEnd);

while(true)
{
listenerSocket.Listen(0);
Socket clientSocket = listenerSocket.Accept();

// runs the clients as threads

Thread clientThead = new Thread((c)=> ClientConnection(clientSocket));


clientThead.Start();
}

}
private static void ClientConnection(Socket clientSocket)
{

byte[] Buffer = new byte[clientSocket.SendBufferSize];

int readByte;
do
{
// received data
readByte = clientSocket.Receive(Buffer);

// do stuff
byte[] rData = new byte[readByte];
Array.Copy(Buffer, rData, readByte);
Console.WriteLine("We got " + Encoding.UTF8.GetString(rData));

//replay
clientSocket.Send(new byte[4] { 65, 66, 67, 68 });

} while (readByte > 0);

Console.WriteLine("Client disconnected");

Console.ReadKey();
}
}
}

You might also like