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