130 likes | 276 Vues
Socket Programming. Laboratory - 4. Overview. TcpListener Class 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 a TcpListener . TCPClient Class
E N D
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