[go: up one dir, main page]

0% found this document useful (0 votes)
24 views3 pages

TCP

The document contains code for a simple chat application with one server and four client classes. The Client class connects to the server, starts read and write threads to send and receive messages. The Server class accepts up to four connections, starts a thread for each to broadcast received messages to other clients.

Uploaded by

Aye Tin Zar Kyaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

TCP

The document contains code for a simple chat application with one server and four client classes. The Client class connects to the server, starts read and write threads to send and receive messages. The Server class accepts up to four connections, starts a thread for each to broadcast received messages to other clients.

Uploaded by

Aye Tin Zar Kyaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

src/chat/chat.

java

package chat;
/*
* @author Rakibul Islam
*/
public class Chat {
public static void main(String[] args) {
System.out.println("Simple chat with 4 clients by RKB");
}
}
********************************

src/chat/client.java
_____________________

package chat;

import java.net.*;
import java.io.*;

public class Client {


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

InetAddress ip = InetAddress.getLocalHost();
System.out.println(ip);
Socket clientsocket = new Socket(ip, 1234);

BufferedReader inFromServer = new BufferedReader(new


InputStreamReader(clientsocket.getInputStream()));
DataOutputStream outToServer = new
DataOutputStream(clientsocket.getOutputStream());

CThread write = new CThread(inFromServer, outToServer, 0);


CThread read = new CThread(inFromServer, outToServer, 1);
write.join();
read.join();

clientsocket.close();
}
}

class CThread extends Thread{


BufferedReader inFromServer;
DataOutputStream outToServer;
int flag;

public CThread(BufferedReader in, DataOutputStream out, int f){


inFromServer = in;
outToServer = out;
flag = f;
start();
}

@Override
public void run(){
String input;
try{
while(true){
if(flag==0){
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
input = inFromUser.readLine();
outToServer.writeBytes(input + '\n');
}
else if(flag==1){
input = inFromServer.readLine();
String values[] = input.split("%id%");
System.out.println("From " + values[1] + ": " + values[0]);
}
}
}catch(Exception e){
System.out.println("Error in Client Thread: "+e);
}
}
}

*****************************************************************

src/chat/server.java
____________________

package chat;

import java.io.*;
import java.net.*;

public class Server {


public static void main(String[] args) throws Exception{
ServerSocket serversocket = new ServerSocket(1234);
System.out.println(serversocket.isClosed());

Socket clientsocket[] = new Socket[4];


BufferedReader inFromClient[] = new BufferedReader[4];
DataOutputStream outToClient[] = new DataOutputStream[4];

for(int i=0; i<4; i++){


System.out.println("Waiting...");
clientsocket[i] = serversocket.accept();
System.out.println("connected "+i);

inFromClient[i] = new BufferedReader(new


InputStreamReader(clientsocket[i].getInputStream()));
outToClient[i] = new
DataOutputStream(clientsocket[i].getOutputStream());
}

SThread thread[] = new SThread[4];


for(int i=0; i<4; i++){
thread[i] = new SThread(inFromClient[i], outToClient, i);
}
for(int i=0; i<4; i++){
thread[i].join();
}
}
}
class SThread extends Thread{
BufferedReader inFromClient;
DataOutputStream outToClient[];
int srcId;
String input;

public SThread(BufferedReader in, DataOutputStream out[], int id){


inFromClient = in;
outToClient = out;
srcId = id;
start();
}

@Override
public void run(){
try{
while(true){
input = inFromClient.readLine();
System.out.println("From client " + srcId + ": " + input);

for(int i=0; i<4; i++){


if(i!=srcId) outToClient[i].writeBytes(input + "%id%" + srcId +
'\n');
}
}
}catch(Exception e){
System.out.println("Error in Server Thread: "+e);
}
}
}

You might also like