server that accepts multiple client - not working - java

Below is the code where there is a server to accept multiple client connections and respond. The server is able to receive the client's message but client is not receiving server messages. I have used multi threading concept on the server.
I also observed that nothing works (even a println statement) beyond line marked with ####. Could be that client is blocked.. Any thoughts?
server code:
public static void main(String argv[]) throws Exception
{
ServerSocket welcomeSocket = new ServerSocket(10000);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
Thread t = new Thread(new acceptconnection(connectionSocket));
t.start();}}
class acceptconnection implements Runnable{
BufferedReader inFromClient,inn;
DataOutputStream ds;
Socket clientsocket;
//constructor
acceptconnection (Socket socket) throws IOException{
this.clientsocket = socket;
inn = new BufferedReader (new InputStreamReader(System.in));
inFromClient =new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
ds = new DataOutputStream(clientsocket.getOutputStream());
public void run (){
try {
String clientSentence, inp;
while(( clientSentence = inFromClient.readLine())!=null)
{
System.out.println("from client" + clientSentence);
ds.writeBytes("hi from server");**// THIS DOES NOT WORK**
}
}
Client code:
public static void main(String argv[]) throws Exception
{
Socket clientSocket;
while(true)
{
// clientSock
clientSocket = new Socket("localhost", 10000);
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("Enter something:");
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');// THIS WORKS - thats why server receives it
**####** modifiedSentence = inFromServer.readLine();**// THIS DOES NOT WORK -client unable to receive**
System.out.println("FROM SERVER: " + modifiedSentence + "remote sock add: "+ clientSocket.getRemoteSocketAddress());

As you're using BufferedReader.readLine() in your client, make sure to use a newline character when writing data out:
ds.writeBytes("hi from server\n");
And, as stated already, remember to flush...
ds.flush();

You should flush the stream on the server side
ds.writeBytes("hello world".getBytes());
ds.flush();

Related

Two client sending message to server in java

This is a simple java program that sends a string from client to server and the server returns the length of the string to the client.
First, I run my server, and then I run a client, type in a string but not pressing Enter, then I run the 2nd client, type in a string and press Enter. Then the second client doesn't get a response until the first client's Enter is pressed. How can I solve that problem?
TCP Client
public class TCPClient {
public static void main(String[] args) throws Exception {
String sentence;
String sentenceLength;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("mycomputer", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
sentenceLength = inFromServer.readLine();
System.out.println(sentenceLength);
clientSocket.close();
}
}
TCP Server
public class TCPServer {
public static void main(String[] args) throws Exception {
String clientSentence;
int clientSentenceLength;
ServerSocket welcomeSocket = new ServerSocket(6789);
while (true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
clientSentenceLength = clientSentence.length();
outToClient.writeBytes(clientSentenceLength + "" + '\n');
}
}
}
As Martin Wickman has indicated in his comment, you need a separate thread. Here's what's happening.
Your server accepts a connection. It now won't do anything else until it has handled that connection.
So your client1 opens the connection to the server. This basically hogs the server (because the server isn't multithreaded or in any other way handling more than one client at a time). The server won't do anything else until it's done with client1.
To fix this, you need to create a thread to handle the client conversation. So your main loop accepts a fresh connection and feeds it to a brand new thread. That thread then handles that specific connection, and the main loop goes back to waiting for another client.
The server waits for the first client to send a sentence. This is the blocking line: clientSentence = inFromClient.readLine();
Only after the first client has send the message, the code will continue and only then the second client is actually handled by Socket connectionSocket = welcomeSocket.accept()
The way to solve is handle each client in its own thread. Simple raw untested example code:
public class ClientHandler implements Runnable {
private Socket socket;
public ClientHandler(Socket socket) {
this.socket = socket;
}
public void run() {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
clientSentence = inFromClient.readLine();
clientSentenceLength = clientSentence.length();
outToClient.writeBytes(clientSentenceLength + "" + '\n');
socket.close();
}
}
En then in the server, after you get the Socket from the accept() call you start a new Thread to handle the client:
while (true) {
Socket connectionSocket = welcomeSocket.accept();
new Thread(new ClientHandler(connectionSocket)).start();
}

TCP Server only receives one message

I am trying to create a simple TCP server and client. I want the client to be able to send multiple messages by only opening the socket once. I have looked at similar questions here, here, and here but they haven't been much use.
My code is a follows:
SampleServerTCP.java
public class SampleServerTCP {
private static final int DEFAULT_PORT_NUMBER = 39277;
public static void main(String[] args) throws IOException {
ServerSocket defaultSocket = new ServerSocket(DEFAULT_PORT_NUMBER);
System.out.println("Listening on port: " + DEFAULT_PORT_NUMBER);
while (true){
Socket connectionSocket = defaultSocket.accept();
BufferedReader fromClient= new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
String msg = fromClient.readLine();
System.out.println("Recieved: " + msg);
}
}
}
TCPClientTest.java
public class TCPClientTest {
public static void main(String args[]) throws UnknownHostException, IOException, InterruptedException{
Socket clientSocket = new Socket("localhost", 39277);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
int c = 0;
while(c<10){
outToServer.writeBytes(c + "\n");
outToServer.flush();
c++;
Thread.sleep(500);
}
clientSocket.close();
}
}
The only output I get is:
Listening on port: 39277
Recieved: 0
Where am I going wrong?
Your problem lies here:
ServerSocket defaultSocket = new ServerSocket(DEFAULT_PORT_NUMBER);
System.out.println("Listening on port: " + DEFAULT_PORT_NUMBER);
while (true){
Socket connectionSocket = defaultSocket.accept();
BufferedReader fromClient= new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
String msg = fromClient.readLine();
System.out.println("Recieved: " + msg);
}
You are opening the socket, reading only one line and then you are waiting for the next socket.
Instead you should do Socket connectionSocket = defaultSocket.accept(); outside your while loop, and read from this socket in your loop, like this:
System.out.println("Listening on port: " + DEFAULT_PORT_NUMBER);
Socket connectionSocket = defaultSocket.accept();
BufferedReader fromClient= new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
String msg = "";
while ((msg = fromClient.readLine()) != null){
System.out.println("Recieved: " + msg);
}
Change your server side code like below
public class SampleServerTCP {
private static final int DEFAULT_PORT_NUMBER = 39277;
public static void main(String[] args) throws IOException {
ServerSocket defaultSocket = new ServerSocket(DEFAULT_PORT_NUMBER);
System.out.println("Listening on port: " + DEFAULT_PORT_NUMBER);
Socket connectionSocket = defaultSocket.accept();
BufferedReader fromClient= new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
String msg = fromClient.readLine();;
while (msg!=null){
System.out.println("Received: " + msg);
msg = fromClient.readLine();
}
}
}

server/client chat socket in java

I have a server/client socket connection, each side can send a message to the other. The client who must begin the chat. I want to close the connection when one of the both sides (server and client) sends "quit" message.
here is my code:
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
while(true){
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
if(modifiedSentence.equals("quit\n")) clientSocket.close();
}
}
}
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String sentence;
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("FROM CLIENT: " + clientSentence);
//capitalizedSentence = clientSentence.toUpperCase() + '\n';
sentence = inFromUser.readLine();
outToClient.writeBytes(sentence);
}
}
}
Any help? :)
You have already written one line. Write another for "quit" and check this string when reading and close().
if you don't understand what you program is doing, use your debugger, that is what it is for.

Java TCP sending first message, then infinite wait

My Issue based on code below:
Run TCPServer.java
then Run TCPClient.java
I expect to have the client print out
Server Said(1): HEY DUDE 1
Server Said(2): HEY DUDE 2
... but it just stays on HEY DUDE 1. What am I doing that is not producing the results I want?
TCPServer.java
import java.io.*;
import java.net.*;
class TCPServer {
public static void main (String args[]) throws Exception{
new TCPServer();
}
TCPServer() throws Exception{
//create welcoming socket at port 6789
ServerSocket welcomeSocket = new ServerSocket(6789);
while (true) {
//block on welcoming socket for contact by a client
Socket connectionSocket = welcomeSocket.accept();
// create thread for client
Connection c = new Connection(connectionSocket);
}
}
class Connection extends Thread{
Socket connectionSocket;
Connection(Socket _connectionSocket){
connectionSocket = _connectionSocket;
this.start();
}
public void run(){
try{
//create input stream attached to socket
BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connectionSocket.getInputStream()));
//create output stream attached to socket
PrintWriter outToClient = new PrintWriter(new OutputStreamWriter(connectionSocket.getOutputStream()));
//read in line from the socket
String clientSentence = inFromClient.readLine();
System.out.println("Client sent: "+clientSentence);
//process
String capitalizedSentence = clientSentence.toUpperCase() + '\n';
//write out line to socket
outToClient.print(capitalizedSentence);
outToClient.flush();
}catch(Exception e){}
}
}
}
TCPClient.java
import java.io.*;
import java.net.*;
class TCPClient {
//String name="";
String host = "localhost";
int port = 6789;
Socket socket = null;
public static void main(String args[]) throws Exception{
TCPClient client = new TCPClient();
client.SendToServer("Hey dude 1");
System.out.println("Server Said(1): "+client.RecieveFromServer());
client.SendToServer("Hey dude 2");
System.out.println("Server Said(2): "+client.RecieveFromServer());
client.close();
}
TCPClient(String _host, int _port) throws Exception{
host = _host;
port = _port;
socket = new Socket(host, port);
}
TCPClient() throws Exception{
socket = new Socket(host, port);
}
void SendToServer(String msg) throws Exception{
//create output stream attached to socket
PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
//send msg to server
outToServer.print(msg + '\n');
outToServer.flush();
}
String RecieveFromServer() throws Exception{
//create input stream attached to socket
BufferedReader inFromServer = new BufferedReader(new InputStreamReader (socket.getInputStream()));
//read line from server
String res = inFromServer.readLine(); // if connection closes on server end, this throws java.net.SocketException
return res;
}
void close() throws IOException{
socket.close();
}
}
Your server thread ends as soon as you process first message. You need to put server code into a loop like this:
String clientSentence;
while ((clientSentence = inFromClient.readLine()) != null) {
System.out.println("Client sent: "+clientSentence);
//process
String capitalizedSentence = clientSentence.toUpperCase() + '\n';
//write out line to socket
outToClient.print(capitalizedSentence);
outToClient.flush();
}

java DataOutputStream exception

The exception is thrown in line 24 the second time I type something (after I have typed the host name) - server works right. Code
import java.io.*;
import java.net.*;
class TCPclient {
public static void main(String[] args) throws Exception {
String hostname, msg;
InetAddress hostaddress;
BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Please type host\n");
hostname = inFromUser.readLine(); //I type localhost
hostaddress = InetAddress.getByName(hostname);
Socket cSocket = new Socket(hostaddress, 44444);
String cAddress = cSocket.getLocalSocketAddress().toString();
DataOutputStream outToServer = new DataOutputStream (cSocket.getOutputStream());
while (true)
{
msg = inFromUser.readLine();
System.out.println(msg);
if (msg.equals("exit"))
{
System.out.println("exit");
break;
}
outToServer.writeBytes(cAddress + " said : " + msg + '\n'); //this line throws an exception the second time it runs
}
cSocket.close();
}
}
I am new in java so I am missing something obvious I guess. Exception reads :
Exception in thread "main"
java.net.SocketException: Software
caused connection abort: socket write
error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:115)
at java.io.DataOutputStream.writeBytes(DataOutputStream.java:259)
at TCPclient.main(TCPClient.java:52) Java
Result: 1
Server :
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception {
String clientSentence;
ServerSocket welcomeSocket = new ServerSocket(44444);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream( ) ) );
clientSentence = inFromClient.readLine();
System.out.println(clientSentence + "\n");
}
}
}
Your client creates one socket and writes over and over again to that one socket. Your server, on the other hand, does this:
ServerSocket welcomeSocket = new ServerSocket(44444);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
That accepts the incoming connection, reads one line, and then abandons it (and I'm guessing on the socket's finalize when being garbage collected it closes the connection). Then it waits for a new connection.
So to fix your immediate problem, try moving
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream( ) ) );
before the while loop.
How long do you wait between typing second line? It might have something to do with socket being idle.
Also with the server code like this you will see only first message. Try this:
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception {
String clientSentence;
ServerSocket welcomeSocket = new ServerSocket(44444);
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
while (true) {
clientSentence = inFromClient.readLine();
System.out.println(clientSentence + "\n");
}
}
}
Try:
while (true)
{
if(inFromUser.readLine() != null)
{
msg = inFromUser.readLine();
System.out.println(msg);
if (msg.equals("exit"))
{
System.out.println("exit");
break;
}
outToServer.writeBytes(cAddress + " said : " + msg + "\n");
}
}
Note the changes:
if(inFromUser.readLine() != null)
{
and
... "\n");
not
... '\n');
Give it a shot. It's probably too simple a solution, but it's something :)

Categories