Professional Documents
Culture Documents
Fundamental Network Programming: Socket in C#
Fundamental Network Programming: Socket in C#
Programming
Socket in C#
I. Review Socket
II. Socket in C#
I. C# Socket class
transport transport
network network controlled
link
by OS
link Internet
physical physical
Socket programming
Two socket types for two transport services:
UDP: unreliable datagram
TCP: reliable, byte stream-oriented
Application Example:
1. client reads a line of characters (data) from its keyboard and sends
data to server
2. server receives the data and converts characters to uppercase
3. server sends modified data to client
4. client receives modified data and displays line on its screen
Socket programming with UDP
UDP: no “connection” between client
and server:
• no handshaking before sending data
• sender explicitly attaches IP destination
address and port # to each packet
• receiver extracts sender IP address and
port# from received packet
write reply to
serverSocket read datagram from
specifying clientSocket
client address,
port number close
clientSocket
Example app: UDP client
UDPClient based on C# Socket
create UDP socket for 1. Socket socket
sending to server
= new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
2. IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.2.255"),
11000);
attach server name, port to message; send into socket 3. byte[] send_buffer = Encoding.ASCII.GetBytes(“Text to send”);
4. socket.SendTo(send_buffer, ep);
read reply characters from socket into string 5. Byte[] buffer = new byte[1024];
6. socket.Receive(buffer);
print out received string and close socket 7. String data = Encoding.ASCII.GetString(buffer);
Example app: UDP server
UDPServer based on c#
create UDP socket 1. UdpClient listener = new UdpClient(11000);
2. IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
3. string received_data;
bind socket to local port number 11000 4. byte[] receive_byte_array;
loop forever 5. while (true)
{
Read from UDP socket into message, getting
receive_byte_array = listener.Receive(ref groupEP);
client’s address (client IP and port)
received_data = Encoding.ASCII.GetString(receive_byte_array, 0,
receive_byte_array.Length);
}
6. listener.Close();
Socket programming with TCP
Client must contact server when contacted by client, server
• server process must first be running TCP creates new socket for server
• server must have created socket
process to communicate with that
(door) that welcomes client’s particular client
contact • allows server to talk with multiple
clients
Client contacts server by: • source port numbers used to
• Creating TCP socket, specifying IP distinguish clients (more in Chap 3)
address, port number of server
process Application viewpoint
• when client creates socket: client TCP provides reliable, in-order
TCP establishes connection to byte-stream transfer (“pipe”)
server TCP between client and server
processes
Client/server socket interaction: TCP
server (running on hostid) client
create socket,
port=x, for incoming
request:
serverSocket = socket()
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
Example app: TCP client
TCPClient based on C# Socket