Java chat application using TCP protocol for Client and Server

Simple Java chat application using TCP protocol with coeds for Client and Server to send Messages in string format

Code:


Server Code:

package clientserver;

import java.io.*;

import java.net.*;

class server

{

public static void main(String[] args) throws Exception

{

ServerSocket sersock = new ServerSocket(3000);

Socket sock = sersock.accept( );

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

OutputStream ostream = sock.getOutputStream();

PrintWriter pwrite = new PrintWriter(ostream, true);

InputStream istream = sock.getInputStream();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

String receiveMessage, sendMessage;

while(true)

{

if((receiveMessage = receiveRead.readLine()) != null)

{

System.out.println("Client:"+receiveMessage);

}

sendMessage = keyRead.readLine();

pwrite.println(sendMessage);

pwrite.flush();

}

}

}

Client Code:

package clientserver;

import java.io.*;

import java.net.*;

public class client

{

public static void main(String[] args) throws Exception

{

Socket sock = new Socket("localhost", 3000);

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

OutputStream ostream = sock.getOutputStream();

PrintWriter pwrite = new PrintWriter(ostream, true);

InputStream istream = sock.getInputStream();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

String receiveMessage, sendMessage;

while(true)

{

sendMessage = keyRead.readLine();

pwrite.println(sendMessage);

pwrite.flush();

if((receiveMessage = receiveRead.readLine()) != null)

{

System.out.println("Server:"+receiveMessage);

}

}

}

}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Java Program to Implement sorting algorithm using TCP on Server application

C++ program to perform data transformation Min-max and Z score Normalization