Socket Programming Laboratory: Overview of TCP Client and Server Implementation
This overview introduces key classes in socket programming, focusing on the TcpListener and TcpClient classes for creating TCP servers and clients. It explains how to listen for and accept incoming connections in blocking synchronous mode and the data transmission methods available in the NetworkStream class. Examples include EchoServerTCP and EchoClientTCP, with exercises to enhance functionality through word conversion and chat programs. Additionally, the guide covers file transfer implementations, showcasing the versatility of socket programming.
Socket Programming Laboratory: Overview of TCP Client and Server Implementation
E N D
Presentation Transcript
Socket Programming Laboratory - 4
Overview • TcpListenerClass • Provides simple methods that listen for and accept incoming connection requests in blocking synchronous mode. You can use either a TcpClient or a Socket to connect with aTcpListener. • TCPClientClass • Provides client connections for TCP network services. • NetworkStreamClass • Provides methods for sending and receiving data over Stream sockets in blocking mode.
TCP Server • Example : EchoServerTCP.cs • TcpListener class constructor public TcpListener(int) public TcpListener(IPEndPoint) public TcpListener(IPAddress, int) • Start listening for connection public void Start() public void Start(int) • Accept incoming connection public TcpClientAcceptTcpClient() • Stop listener public void Stop()
TCP Client • Example : EchoClientTCP.cs • TcpClient class constructor public TcpClient() public TcpClient(AddressFamily) public TcpClient(IPEndPoint) public TcpClient(string, int) • Data transfer using NetworkStream class public NetworkStreamGetStream() • Receive Data public override int Read(byte[], int, int) • Send Data public override void Write(byte[], int, int)
Example 1 • EchoServerTCP.cs and EchoClientTCP.cs
Exercise 1 • Modify the EchoServerTCP.cs program that the server will reply by capitalising each word, and convert the remaining letters to lower case as shown below: • Input> all in lowercase • Reply> All In Lowercase • Input> mIxEDdaTACaSeS • Reply> Mixed Data Cases
Data Encoding • Example : CharEncoding.cs • GetBytes(), GetString() method • Message is sent over the network 1 byte at a time • Encoding technique • ASCII • Unicode
Example 2 • Uppercase Conversion Program • UppercaseServer.cs, UppercaseClient.cs
Exercise 2 • Instead of using only TcpListener class and TcpClient class, change the upper case conversion program using only Socket Class implementation.
Example 3 • Simple Chat Program • ChatServer.cs, ChatClient.cs
Exercise 3 • Modify the ChatServer.cs program using only Socket Class implementation instead of using TcpListenerclass and TcpClient class
Example 4 • File Transfer Program • FileServer.cs, FileClient.cs