Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Client-server-client chat server

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Quick! Before anybody notices! Cover it up with this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic