1

I have a program ready that acts as a client and another that acts as a server.

I want to be able to send a message to the server, and the server will then forward the message to another client that is also connected to the server.

So the the server is supposed to forward the message to the other client.

How would i be able to do that, and what do I need to read up on?

This is what i got now.

Server.java

package server;

import java.net.*;
import java.io.*;

import javax.swing.*;

public class Server{

public static void run() {
    ServerSocket serverSocket = null;
    Socket socket = null;
    try{
        while(true){
            serverSocket = new ServerSocket(5555);
            socket = serverSocket.accept();
            InputStreamReader streamReader = new InputStreamReader(socket.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(streamReader);
            String message = bufferedReader.readLine();
            System.out.println(message);
            if(message != null){
                PrintStream printStream = new PrintStream(socket.getOutputStream());
                printStream.println("Message receivd!");
            }
            streamReader.close();
            socket.close();
            serverSocket.close();
            bufferedReader.close();
        }
    }catch(Exception e){}
 //     try{
 //         
 //     }catch(Exception e){}
     }
    public static void main(String[]args){
    Server s = new Server();
    s.run();
}

}

And then I also got TCPClient.java

package client;

 import java.net.*;
 import java.io.*;

 public class TCPClient {
private String serverIP = "localhost";
private int serverPort = 1111;
private int count = 0;
private Thread thread;

public TCPClient() {
    this.thread = new Thread(new ConnectAndListenToServer());
  //        thread.start();
}
public void sendMessage(int count){
    this.count = count;
    this.thread = new Thread(new ConnectAndListenToServer());
    thread.start();
}
private class ConnectAndListenToServer implements Runnable {
    Socket socket = null;
    public void run() {
        BufferedReader bufferedReader = null;
        InputStreamReader streamReader = null;
        try {
            socket = new Socket(serverIP,serverPort);
            PrintStream printStream = new PrintStream(socket.getOutputStream());
            printStream.println(count);
            streamReader = new InputStreamReader(socket.getInputStream());
            bufferedReader = new BufferedReader(streamReader);
            String message = bufferedReader.readLine();
            if(message != null){
                System.out.println(message);
            }

        }catch(Exception e){}
        try{
            socket.close();
            bufferedReader.close();
            streamReader.close();
        }catch(Exception ee){}
    }
}

}

How would i be able to forward the messeage already received on the server to another client?

Example

theOGloc
  • 231
  • 4
  • 13
  • You need to use `ServerSocket` and `Socket` classes. I suppose that you want to send messages over TCP. – PetrS Apr 10 '14 at 23:17

2 Answers2

4

How would i be able to forward the messeage already received on the server to another client?

I have already posted some samples in the same context.

Till now you have done well, to complete it please have a look Client-Server Communication. I have described it step by step just follow the thread to find other samples as well.

Please let me know if still you have any issue!

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
0

This just works if you have Client 2 connected while Client 1 is also connected.

This is possible if you write a multithreading application or use a Selector.

mjb4
  • 989
  • 1
  • 9
  • 14