I recently wrote a socket communication program in java where two threads run concurrent on each server and client side handling the read and write operations to the socket allowing continuous message sending and receiving on both sides.
The problem is either of the client or the server stops receiving communication from the other side and then after a while both of them stop working. I can't figure out what's wrong and how the connection is dropping :/
Code for server
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Server
{
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
private DataOutputStream out = null;
private Scanner inp = null;
String line = "";
String iline = "";
public Server(int port)
{
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
out=new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(new
BufferedInputStream(socket.getInputStream()));
inp = new Scanner(System.in);
while (true)
{
new Thread(new Runnable(){
public void run()
{
try{
while(true){
line = in.readUTF();
System.out.println("Client : "+line);
if(socket.isClosed()||socket.isOutputShutdown()||socket.isInputShutdown())
{
System.out.println("DED");
System.exit(0);
}
}
}
catch(Exception e){
System.out.println("Exception !!!");
}
}
}).start();
iline=inp.nextLine();
out.writeUTF(iline);
if(socket.isClosed()||socket.isOutputShutdown()||socket.isInputShutdown()){
System.out.println("DED");
System.exit(0);
}
}
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Server server = new Server(5000);
}
}
Code for Client
import java.net.*;
import java.io.*;
import java.util.Scanner;
class Client{
private Socket socket =null;
private DataInputStream inp=null;
private DataOutputStream out=null;
private Scanner in=null;
String line="";
String iline="";
Client(String address,int port)
{
try{
socket = new Socket(address,port);
in= new Scanner(System.in);
out = new DataOutputStream(socket.getOutputStream());
inp = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
while(true){
line=in.nextLine();
out.writeUTF(line);
new Thread(new Runnable(){
public void run()
{
try{
while(true){
iline=inp.readUTF();
System.out.println("Server : "+iline);
if(socket.isClosed()||socket.isOutputShutdown()||socket.isInputShutdown()){
System.out.println("DED");
System.exit(0);
}
}
}
catch(Exception e){
System.out.println("Exception !!!");
}
}
}).start();
if(socket.isClosed()||socket.isOutputShutdown()||socket.isInputShutdown()){
System.out.println("DED");
System.exit(0);
}
}
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(Exception e){
System.out.println("EXCEPTION!!!");
}
}
}
class ClientSocket{
public static void main(String...args){
Client obj = new Client("127.0.0.1", 5000);
}
}
Just an initial run through your code what I see is in the first while(true){} , you are spawning a thread calling start method on it . The moment you start the reading thread the main thread checks fo sockets conditions and moves ahead. Since there is a true in your first while(true) a new thread is again spawned and that goes on an on until the socket is closed where the programs terminates because of system.exit call.
Related
my server class
import java.net.*;
import java.io.*;
public class server
{
private Socket socket;
private ServerSocket server;
// constructor with port
public void start(int port){
try {
server = new ServerSocket(port);
while(true){
socket = server.accept();
new ConnectionHandler(socket).run();
}
}catch(IOException i){
}
}
}
class ConnectionHandler implements Runnable{
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
public ConnectionHandler(Socket socket){
this.socket=socket;
}
#Override
public void run() {
InputStream inp = null;
BufferedReader brinp = null;
DataOutputStream out = null;
try
{
System.out.println("Waiting for a client ...");
System.out.println(server);
System.out.println("Client accepted");
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String line = "";
// reads message from client until "Over" is sent
while (!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String[] args) {
server serverr = new server();
serverr.start(4000);
}
}
Here's my client class.
import java.net.*;
import java.io.*;
public class Client
{
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
// constructor to put ip address and port
public Client(String address, int port)
{
// establish a connection
try
{
socket = new Socket(address, port);
System.out.println("Connected");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
// string to read message from input
String line = "";
// keep reading until "Over" is input
while (!line.equals("Over"))
{
try
{
line = input.readLine();
out.writeUTF(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
// close the connection
try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Client client = new Client("127.0.0.1", 4000);
}
}
Trying to develop pretty simple chat application works via terminal, but I think there are plenty of bugs I have in my code.
The server can handle one client, but when another client comes up it doesn't connect to other clients.
What am I have to do now?
I couldn't find out where my problem is, waiting your helps.
Note: I am completely new to socket programming concept.
The ConnectionHandler class is a thread class, and you must wrap its object to a Thread instance and then call start() instead of run().
So in the Server class change
new ConnectionHandler(socket).run();
with
new Thread(ConnectionHandler(socket)).start();
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I wrote a java application for both Server and Client. What I want to do is stop the Client's application(and all of it's threads) when the user enters the word: "logout". I've tried everything I could find so kinda desperate here. Please send help!
Here is my code for Client.java
package client;
//Java implementation for multithreaded chat client
//Save file as Client.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client extends Thread
{
final static int ServerPort = 1234;
private volatile static boolean running = true;
public static void main(String args[]) throws UnknownHostException, IOException
{
Scanner scn = new Scanner(System.in);
// getting localhost ip
InetAddress ip = InetAddress.getByName("localhost");
// establish the connection
Socket s = new Socket(ip, ServerPort);
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// sendMessage thread
Thread sendMessage = new Thread(new Runnable()
{
#Override
public void run() {
while (running) {
// read the message to deliver.
try {
String msg = scn.nextLine();
if(msg == "logout") {
running = false;
dis.close();
dos.close();
scn.close();
s.close();
Thread.currentThread().interrupt();
break;
}
dos.writeUTF(msg);
}
catch (IOException e) {
if(!running) {
System.out.println("Closing...");
System.exit(0);
}
}
} }
});
// readMessage thread
Thread readMessage = new Thread(new Runnable()
{
#Override
public void run() {
while (running) {
// read the message sent to this client
try {
String msg = dis.readUTF();
if(sendMessage.isInterrupted()) {
running = false;
dis.close();
dos.close();
scn.close();
s.close();
Thread.currentThread().interrupt();
break;
}
System.out.println(msg);
} catch (IOException e) {
if(!running) {
System.out.println("Closing...");
System.exit(0);
}
}
}
}
});
sendMessage.start();
readMessage.start();
}
}
And this is my Server.java
package server;
//Java implementation of Server side
//It contains two classes : Server and ClientHandler
//Save file as Server.java
import java.io.*;
import java.util.*;
import java.net.*;
//Server class
public class Server
{
// Vector to store active clients
static Vector<ClientHandler> ar = new Vector<>();
// counter for clients
static int i = 0;
public static void main(String[] args) throws IOException
{
// server is listening on port 1234
ServerSocket ss = new ServerSocket(1234);
Socket s;
// running infinite loop for getting
// client request
while (true)
{
// Accept the incoming request
s = ss.accept();
System.out.println("New client request received : " + s);
// obtain input and output streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("Creating a new handler for this client...");
// Create a new handler object for handling this request.
ClientHandler mtch = new ClientHandler(s,"client " + i, dis, dos);
// Create a new Thread with this object.
Thread t = new Thread(mtch);
System.out.println("Adding this client to active client list");
// add this client to active clients list
ar.add(mtch);
// start the thread.
t.start();
// increment i for new client.
// i is used for naming only, and can be replaced
// by any naming scheme
i++;
}
}
}
//ClientHandler class
class ClientHandler implements Runnable
{
private String name;
final DataInputStream dis;
final DataOutputStream dos;
Socket s;
boolean isloggedin;
// constructor
public ClientHandler(Socket s, String name,
DataInputStream dis, DataOutputStream dos) {
this.dis = dis;
this.dos = dos;
this.name = name;
this.s = s;
this.isloggedin=true;
}
#Override
public void run() {
String received;
while (true)
{
try
{
// receive the string
received = dis.readUTF();
if(received.equals("logout")){
break;
}
// break the string into message and recipient part
StringTokenizer st = new StringTokenizer(received, "#");
String MsgToSend = st.nextToken();
String recipient = st.nextToken();
// search for the recipient in the connected devices list.
// ar is the vector storing client of active users
for (ClientHandler mc : Server.ar)
{
// if the recipient is found, write on its
// output stream
if (mc.name.equals(recipient) && mc.isloggedin==true)
{
mc.dos.writeUTF(this.name+" : "+MsgToSend);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
try
{
// closing resources
this.dis.close();
this.dos.close();
this.s.close();
this.isloggedin=false;
}catch(IOException e){
e.printStackTrace();
}
}
}
Code reference: Multithread GroupChat 1
Multithread GroupChat 2
Don't compare Strings with == but with equals(). msg == "logout" Should be msg.equals("logout").
Can someone look at my code and tell me what's wrong?
I am trying to make a client server chat program and want server to accept endless connections requested from the clients. It runs successfully. At First run, server and client are able to chitchat properly but when the client is closed(quit) and again came back, client is able to connect with
server and even it can send messages to server and messages are also received by the server BUT there is issue at server side, At Server side server messages are not reach to client properly. Some messages are skipped and are not able to reached to client immediately as it was before in the first run.Please help me if anyone can find this solution.
SERVER CODE:
//packages
import java.io.*;
import java.net.*;
import java.lang.*;
public class Server1 {
public static void main(String[] args) throws IOException {
// Instance variable fields
ServerSocket listener=null;
Socket clientSocket=null;
final int port = 444;
System.out.println("Server waiting for connection on port "+port);
try
{
listener = new ServerSocket(port);
// Creates a server socket bound to the specified port.
}
catch(IOException e)
{
e.printStackTrace();
}
while(true)
{
try
{
clientSocket = listener.accept(); //Listens for a connection to be made to this socket and accepts it.
//clientsocket is now the route through which we can communicate with the client.
//if we reach here, the server got a call already...
System.out.println("You have succesfully connected");
/*Returns local address of this serversocket and
Returns the remote port number to which this socket is connected. */
System.out.println("Recieved connection from "+clientSocket.getInetAddress() +" on port "+clientSocket.getPort());
new echoThread(clientSocket).start(); //new thread for a client
}
catch( IOException e)
{
System.out.println("I/O error "+e);;
}
}
}
}
class echoThread extends Thread{
Socket clientSocket; // instance variable field
//For every client we need to start separate thread.
public echoThread(Socket clientSocket) //constructor
{
this. clientSocket = clientSocket;
}
//create two threads to send and recieve from client
public void run()
{
RecieveFrmClientThread recieve = new RecieveFrmClientThread(this.clientSocket);
Thread thread = new Thread(recieve);
thread.start();
SndToClientThread send = new SndToClientThread(this.clientSocket);
Thread thread2 = new Thread(send);
thread2.start();
}
}
//INCOMING
class RecieveFrmClientThread implements Runnable
{
Socket clientSocket=null;
BufferedReader br = null;
public RecieveFrmClientThread(Socket clientSocket)
{
this.clientSocket = clientSocket;
} //end constructor
public void run() {
try{
br = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
//returns the input stream through which the server can hear the client.
String messageString="";
System.out.println("Please enter something to send back to client..");
while(true){
while((messageString = br.readLine())!= null)
{//assign message from client to messageString
if(messageString.equals("EXIT"))
{
System.out.println("you have been logout from it");
//break;//break to close socket if EXIT
}
System.out.println("From Client: " + messageString);//print the message from client
messageString="";
}
}
}
catch(Exception ex){System.out.println(ex.getMessage());}
}
}//end class RecieveFromClientThread
//OUTGOING
class SndToClientThread implements Runnable
{
//instance field variables
PrintWriter pwPrintWriter;
Socket clientSock = null;
public SndToClientThread(Socket clientSock)
{
this.clientSock = clientSock;
}
public void run() {
try{
pwPrintWriter =new PrintWriter(this.clientSock.getOutputStream(),true);//returns the output stream through which the server can talk to the client,
while(true)
{
String msgToClientString = null;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));//get input from server
msgToClientString = input.readLine();//read message from server to send it to client
if(msgToClientString.equals("EXIT"))
{
System.out.println("Disconnecting Client!!");
//System.exit(0);
}
pwPrintWriter.println(msgToClientString);//send message to client with PrintWriter
pwPrintWriter.flush();//flush the PrintWriter
}//end while
}
catch(Exception ex){System.out.println(ex.getMessage());}
}//end run
}//end class SendToClientThread
CLIENT CODE :
import java.io.*;
import java.net.*;
public class Client1 {
//Instance variable field
//private ServerSocket ss;
public static void main(String[] args)
{
try {
Socket sock = new Socket("localhost",444);
/*//create two threads to send and recieve from client
RecieveFromServer recieveThread = new RecieveFromServer(sock);
System.out.println("SOCK=" +sock);
Thread thread2 =new Thread(recieveThread);
thread2.start();
SendToServer sendThread = new SendToServer(sock);
Thread thread = new Thread(sendThread);
thread.start();*/
new echoThread1(sock).start(); //new thread for a client
}
catch (Exception e) {System.out.println(e.getMessage());}
}
}
class echoThread1 extends Thread {
Socket sock;
public echoThread1(Socket sock) {
this.sock = sock;
}
//create two threads to send and recieve from client
public void run()
{
RecieveFromServer recieveThread = new RecieveFromServer(this.sock);
Thread thread = new Thread(recieveThread);
thread.start();
SendToServer sendThread = new SendToServer(this.sock);
Thread thread2 = new Thread(sendThread);
thread2.start();
}
}
class RecieveFromServer implements Runnable
{
Socket sock=null;
BufferedReader recieve=null;
public RecieveFromServer(Socket sock) {
this.sock = sock;
}//end constructor
public void run() {
try{
recieve = new BufferedReader(new InputStreamReader(sock.getInputStream()));//get inputstream for this socket
String msgRecieved = null;
while((msgRecieved = recieve.readLine())!= null)
{
System.out.println("From Server: " + msgRecieved);
}
}catch(Exception e){System.out.println(e.getMessage());}
}//end run
}//end class RecieveFromServer
class SendToServer implements Runnable
{
Socket sock=null;
PrintWriter print=null;
BufferedReader brinput=null;
public SendToServer(Socket sock)
{
this.sock = sock;
}//end constructor
public void run(){
try{
if(this.sock.isConnected()) //Returns TRUE if the socket is successfuly connected to a server
{
System.out.println("Client connected to "+this.sock.getInetAddress() + " on port "+this.sock.getPort());
this.print = new PrintWriter(sock.getOutputStream(), true);
System.out.println("Type your message to send to server..type 'EXIT' to exit");
while(true){
brinput = new BufferedReader(new InputStreamReader(System.in)); // input from client
String msgtoServerString=null;
msgtoServerString = brinput.readLine();
this.print.println(msgtoServerString);
this.print.flush();
if(msgtoServerString.equals("EXIT"))
{
System.out.println("you have logged out of server...Thanks for your visit");
sock.close();
break;}
}
}//end while
}
catch(Exception e){System.out.println(e.getMessage());}
}//end run method
}//end class SendToServer
I am trying to figure out why I can send messages to the client from the server but when I try the other way around (to send messages from the client to the server) the program halts like it is waiting for some action to happen.
also how to prevent the sockets from being closed immediately.
this is the code:
Client Class
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
Client aClient = new Client();
aClient.run();
}
private Socket socket;
private PrintWriter toServer;
private BufferedReader fromServer;
public void run() {
try {
socket = new Socket("localhost", 9000);
if (socket.isConnected()){
System.out.println("CONNECTED");
}
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
toServer = new PrintWriter(socket.getOutputStream());
toServer.print("hello server");
System.out.print(fromServer.readLine());
toServer.close();
socket.close();
} catch (Exception error) {
System.out.println("CLIENT ERROR: " + error);
}
}
}
Server Class
import java.io.*;
import java.net*;
public class Server {
public static void main(String[] args) {
Server aServer = new Server();
aServer.run();
}
private ServerSocket mainSocket;
private Socket socket;
private PrintWriter toClient;
private BufferedReader fromClient;
public Server(){
try{
mainSocket = new ServerSocket(9000);
}
catch (Exception error){
System.out.print("Error :"+error);
}
}
public void run() {
System.out.println("WAITING FOR CLIENTS");
try {
socket = mainSocket.accept();
if(socket.isConnected()) {
System.out.println("CONNECTED.");
}
fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
toClient = new PrintWriter(socket.getOutputStream());
System.out.println(fromClient.readLine());
toClient.print("hello Client");
toClient.close();
}
catch (Exception error) {
System.out.println("SERVER ERROR :" + error);
}
}
}
Add this before toServer.print("hello server"); to Client class. Its read from System.in and send to the Server class:
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromUser;
while((fromUser = stdIn.readLine()) != null) {
System.out.println("Client: " + fromUser);
toServer.println(fromUser);
}
And to Server class add this, before System.out.println(fromClient.readLine());, its read line from client and print to System.out:
String inputLineFromClient;
while ((inputLineFromClient = fromClient.readLine()) != null) {
System.out.println(inputLine);
}
Socket not closing before you invoke method close() or occurs some Exception.
your server is not reading anything.
use
fromClient.readLine();
I have a server which uses multiple threads to accept multiple clients. I have it at the moment that when a client types something, it appears on the server as Client: text This works fine for 1 client. However the problem is when a second client joins.
They join fine and they can type fine. But the first client can only type 1 more thing then they stop, i.e their messages doesn't appear on the server. I presume they can do 1 as the method has been started but doesn't repeat. I've tried a while (true) loop (as seen in the code) and recalling the method at the end, neither work. I'm new to programming so don't have much expertise in this. Please find the code below :) (Note, indention is correct, just hasn't copied across properly)
Server:
package dod;
import java.net.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.*;
public class Server implements Runnable
{
PrintWriter out;
BufferedReader in;
Socket clientSocket;
ServerSocket serverSocket;
int portNumber;
AtomicInteger numClients = new AtomicInteger(0);
public static void main(String[] args)
{
Server s = new Server();
s.startup();
}
public void run()
{}
/**
* Start the server on the user picked port
*/
public void startup()
{
try
{
System.out.println("Enter a port");
Scanner dif = new Scanner(System.in);
portNumber = Integer.parseInt(dif.nextLine());
dif.close();
serverSocket = new ServerSocket(portNumber);
newThread();
}
catch (IOException e) {
System.out.println("Error");
System.exit(0);
}
}
public void newThread()
{
Thread thread =new Thread("C"+numClients.getAndIncrement())
{
public void run()
{
accept();
}
};
thread.start();
}
public void accept()
{
try
{
clientSocket = serverSocket.accept();
System.out.println("A new client has just connected.");
} catch(IOException e)
{
System.out.println("error");
System.exit(0);
}
newThread();
listenCommand();
}
public void listenCommand()
{
while (true)
{
try
{
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String userInput;
while ((userInput = in.readLine()) != null)
{
System.out.println("client: " + userInput);
}
} catch (IOException e)
{
System.out.println("Error");
System.exit(0);
}
}
}
}
Client:
package dod;
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String[] args) throws IOException {
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try {
Socket serverSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(serverSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null)
{
out.println(userInput);
}
} catch(UnknownHostException e) {
System.out.println("error in host");
} catch(IOException e) {
System.out.println("error in IO");
}
}
}
Thank you! :)
*emphasized text*In the Server class you should have a Thread listening for new clients arriving and assigning them their own socket. You have the socket and the streams as a member variables, so everytime a new client comes you replace the socket. Also you open a new Thread for the accept connection, instead for the client itself.
Check the following (See that the client socket is another thread and I created a Runnable for it):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
public class Server
{
ServerSocket serverSocket;
int portNumber;
AtomicInteger numClients = new AtomicInteger(0);
public static void main(String[] args)
{
Server s = new Server();
s.startup();
}
/**
* Start the server on the user picked port
*/
public void startup()
{
try
{
System.out.println("Enter a port");
Scanner dif = new Scanner(System.in);
portNumber = Integer.parseInt(dif.nextLine());
dif.close();
serverSocket = new ServerSocket(portNumber);
newThread();
}
catch (IOException e) {
System.out.println("Error");
System.exit(0);
}
}
public void newThread()
{
Thread thread =new Thread("C"+numClients.getAndIncrement())
{
public void run()
{
while(true) {
try {
accept();
} catch (Exception e) {
// lof the exception
}
}
}
};
thread.start();
}
public void accept()
{
try
{
Socket clientSocket = serverSocket.accept();
new Thread(new ClientSocket(clientSocket)).start();
System.out.println("A new client has just connected.");
} catch(IOException e)
{
System.out.println("User disconnected");
System.exit(0);
}
}
class ClientSocket implements Runnable {
Socket clientSocket;
public ClientSocket(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
{
try
{
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String userInput;
while ((userInput = in.readLine()) != null)
{
System.out.println("client: " + userInput);
}
} catch (IOException e)
{
System.out.println("Error. Probably client disconnected");
// System.exit(0); do you want to exist when a client disconnects?
}
}
}
}
}