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

TCP Client/Server Interaction or Communication

The document describes the interaction between a TCP client and server for communication. It outlines the key steps: 1) The TCP server listens on a port for connection requests from clients. 2) When a client connects, the server accepts the connection and the two can exchange data via read and write functions. 3) The client initiates a connection to the server's port, and then uses read and write functions to exchange data before closing the connection.

Uploaded by

David Rotich
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

TCP Client/Server Interaction or Communication

The document describes the interaction between a TCP client and server for communication. It outlines the key steps: 1) The TCP server listens on a port for connection requests from clients. 2) When a client connects, the server accepts the connection and the two can exchange data via read and write functions. 3) The client initiates a connection to the server's port, and then uses read and write functions to exchange data before closing the connection.

Uploaded by

David Rotich
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

TCP CLIENT/SERVER INTERACTION OR COMMUNICATION

TCP SERVER TCP stands for Transmission Control Protocol and it is a standard protocol data transmission with confirmation of data reception. In order to initiate a TCP session, server and a client are required. Firstly a server is set up to listen at a given port but it waits until it a client attempts to connect to that port. Here are the steps to get TCP server up and running. socket( ) bind( ) +---->listen( ) | accept( ) | (block until connection from client ) | read( ) | write( ) +-----close( ) close( ) Server Code Imports System.Net.Sockets Imports System.Text Module Module1 Dim clientsList As New Hashtable Sub Main() Dim serverSocket As New TcpListener(8888) Dim clientSocket As TcpClient Dim counter As Integer serverSocket.Start() msg("Server Connected ....") counter = 0 While (True) counter += 1 clientSocket = serverSocket.AcceptTcpClient() Dim bytesFrom(10024) As Byte Dim dataFromClient As String Dim networkStream As NetworkStream = _ clientSocket.GetStream()
1

networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize)) dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom) dataFromClient = _ dataFromClient.Substring(0, dataFromClient.IndexOf("$")) clientsList(dataFromClient) = clientSocket broadcast(dataFromClient + " Joined ", dataFromClient, False) msg(dataFromClient + " Joined chat room ") Dim client As New handleClinet client.startClient(clientSocket, dataFromClient, clientsList) End While clientSocket.Close() serverSocket.Stop() msg("exit") Console.ReadLine() End Sub Sub msg(ByVal mesg As String) mesg.Trim() Console.WriteLine(" >> " + mesg) End Sub Private Sub broadcast(ByVal msg As String, _ ByVal uName As String, ByVal flag As Boolean) Dim Item As DictionaryEntry For Each Item In clientsList Dim broadcastSocket As TcpClient broadcastSocket = CType(Item.Value, TcpClient) Dim broadcastStream As NetworkStream = _ broadcastSocket.GetStream() Dim broadcastBytes As [Byte]() If flag = True Then broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg) Else broadcastBytes = Encoding.ASCII.GetBytes(msg) End If broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length) broadcastStream.Flush() Next End Sub Public Class handleClinet
2

Dim clientSocket As TcpClient Dim clNo As String Dim clientsList As Hashtable Public Sub startClient(ByVal inClientSocket As TcpClient, _ ByVal clineNo As String, ByVal cList As Hashtable) Me.clientSocket = inClientSocket Me.clNo = clineNo Me.clientsList = cList Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat) ctThread.Start() End Sub Private Sub doChat() 'Dim infiniteCounter As Integer Dim requestCount As Integer Dim bytesFrom(10024) As Byte Dim dataFromClient As String Dim sendBytes As [Byte]() Dim serverResponse As String Dim rCount As String requestCount = 0 While (True) Try requestCount = requestCount + 1 Dim networkStream As NetworkStream = _ clientSocket.GetStream() networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize)) dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom) dataFromClient = _ dataFromClient.Substring(0, dataFromClient.IndexOf("$")) msg("From client - " + clNo + " : " + dataFromClient) rCount = Convert.ToString(requestCount) broadcast(dataFromClient, clNo, True) Catch ex As Exception MsgBox(ex.ToString) End Try End While End Sub End Class End Module

TCP CLIENT Client Code These are the steps that a client needs to take in order to communicate with the server. socket( ) connect( ) write( ) read( ) close( ) Imports System.Net.Sockets Imports System.Text Public Class Form1 Dim clientSocket As New System.Net.Sockets.TcpClient() Dim serverStream As NetworkStream Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim serverStream As NetworkStream = clientSocket.GetStream() Dim outStream As Byte() = _ System.Text.Encoding.ASCII.GetBytes("Message from Client$") serverStream.Write(outStream, 0, outStream.Length) serverStream.Flush() Dim inStream(10024) As Byte serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize)) Dim returndata As String = _ System.Text.Encoding.ASCII.GetString(inStream) msg("Data from Server : " + returndata) End Sub Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load msg("Client Started") clientSocket.Connect("127.0.0.1", 8888) Label1.Text = "Client Socket Program - Server Connected ..." End Sub Sub msg(ByVal mesg As String) TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + mesg End Sub End Class

You might also like