wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Client-server-client chat server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Client-server-client chat server" Watch "Client-server-client chat server" New topic
Author

Client-server-client chat server

Akshata Alandker
Greenhorn

Joined: Sep 11, 2012
Posts: 19
Hi everyone, i am a beginner in network programming, I have an assignment to develop a simple chat server with 2 clients and server.
This is my code,

server code

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server implements Runnable{
static ServerSocket providerSocket;
static Socket connection = null;
static ObjectOutputStream out;
static ObjectInputStream in;
String message;

Server(){}
//public void run(){
public static void main(String[] args){
int count = 2;

try{
//1. creating a server socket
providerSocket = new ServerSocket(45155);
}catch(IOException ioException){
ioException.printStackTrace();
}
try{
while(true){
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());

//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");
Runnable runnable = new Server();
Thread thread = new Thread(runnable);
thread.start();

}


}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
public void run(){
//4. The two parts communicate via the input and output streams
do{
try{
message = (String)in.readObject();
System.out.println("client>" + message);
if (message.equals("bye")){
sendMessage("bye");
}
Scanner in =new Scanner(System.in);
message = in.nextLine();

sendMessage(message);

}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}while(!message.equals("bye"));

}
static void sendMessage(String msg)
{

try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}

}

client code

import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Requester{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester(){}
void run() throws ClassNotFoundException
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("localhost", 45155);
System.out.println("Connected to localhost in port 45155");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server
getMessage();


}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void getMessage() throws IOException, ClassNotFoundException{
do{
message = (String)in.readObject();
System.out.println("server>" + message);
Scanner in =new Scanner(System.in);
message = in.nextLine();
sendMessage(message);
}while(!message.equals("bye"));
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Requester client = new Requester();
try {
client.run();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

I am facing some problems here,1) the server is accepting 2 connections, but the communication is happening only between server and second client, and after some time I am getting this error

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.peek(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.peek(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.simpragma.server1.Server.run(Server.java:67)
at java.lang.Thread.run(Unknown Source)

2) For only one client the communication with server is correct, and is terminated by 'bye' by the client.
3)My task is to communicate between 2 clients via server, the messages should be printed on client consoles.

can anyone tell me where i should modify the code to get the correct communication done... thanks
Ash Chan
Greenhorn

Joined: Jan 09, 2013
Posts: 3
Disclaimer: Not gone through your entire prog in detail! In a hurry, so try this:

ServerSocket ss = new ServerSocket(portNo);
while (!shutDownRequested) {
Socket s = ss.accept();
Thread thr = new HandlerThread(s);
thr.start();
}
...
private class HandlerThread extends Thread {
private Socket s;
HandlerThread(Socket s) {
this.s = s;
}
public void run() {
// handle connection, reading and writing to s
}
}

I think this is similar to the code you had posted.

I am not sure you handling the clients.

This is something I had done sometime back:

servSocket = new ServerSocket(9141);
while(true){
socket = servSocket.accept();
ClientNum++;
for(int i=0;i<10;i++){//for eg. This can support 10 client
if(threads[i]==null){
sockets[i]=socket;
For each client that comes in, it is accepted.

In a hurry, so not sure how clear this is. Hope it helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Client-server-client chat server
 
Similar Threads
accessing a server using socket connection
Secure Websockets 443 and Java
Reseting socket after few seconds
Why won't my program compile?
a total newbie in Sockets