3

this is not my homework(my homework is just about doing chat with a client and server which it works correctly especially with your help[:-)] but I want to make two clients chat with each other,I don't know that when i get text from the first one how can I send that text to the other client.would you please help me.thanks.

public class MainServer {

static Socket client = null;
static ServerSocket server = null;



public static void main(String[] args) {
    System.out.println("Server is starting...");
    System.out.println("Server is listening...");

    try {
        server = new ServerSocket(5050);
    } catch (IOException ex) {
        System.out.println("Could not listen on port 5050");
        System.exit(-1);

    }
    try {
        boolean done = false;
        while (!done) {

            client = server.accept();
            System.out.println("Client Connected...");
            BufferedReader streamIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintWriter streamOut = new PrintWriter(client.getOutputStream(),true);
            String line = streamIn.readLine();
            if (line.equalsIgnoreCase("bye")) {
                streamIn.close();
                client.close();
                server.close();
                done = true;
            } else {
                System.out.println(line);
                streamOut.println(line);
            }
        }

    } catch (IOException e) {
        System.out.println("IO Error in streams " + e);
    }
}}
Johanna
  • 27,036
  • 42
  • 89
  • 117
  • I say your previous question about making a client talk with a server... It seems you are getting trouble with your home work. Try to do it step by step. 1) clientsends a message to server (client button pressed) 2) server sends a message to client (test it as an echo to the client that sends something... it is, in the code that processes the message client->server, save the line and resend it to client) 3) when 1 and 2 work then try sending between two diferent clients. – helios Jan 18 '10 at 16:09
  • Supposedly you have each client connected to server via a socket so you can have a Map in your server that maps a name or whatever to the corresponding socket. If you send a message from client to server define the format (by example: target_client (\n) message (\n), something simple to start) – helios Jan 18 '10 at 16:11
  • @Johanna: Please start looking at the **preview** when writing/editing your posts. Single line-breaks have no effect. That's why your message is displayed without any format at all... – Peter Lang Jan 18 '10 at 16:26
  • @ helios : I get what you mean could you please do something on this server code for making me more understandable?thanks – Johanna Jan 18 '10 at 17:16
  • https://superuser.com/questions/48343/what-are-inbound-and-outbound-rules-for-windows-firewall -might help – Channa Sep 28 '19 at 19:57

6 Answers6

3

That's it, your two "clients" will both act as client and server : Listening to incoming things on a socket and sending things over an other sockets.

Pico
  • 71
  • 1
  • So it doesn't need to set the server socket to send the text to which client?I mean client A send text to the server and the server should send that text to the clientB ,the server should know which client. – Johanna Jan 18 '10 at 16:07
2

On the server, you can keep a Set of all the clients that are currently connected to the server. The server should listen for messages (can do this with a ServerSocket, and clients connect with normal Sockets). Each time the server receives a message, it sends this message back to all clients in the Set, and the clients display the message.

EDIT: this is for a client-server system, where clients connect to a central server instead of directly to each other. If you want to do direct client-to-client, one of them will just have to act as the server, and you'll need to implement a chat UI in both.

Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113
2

Here is a very simple, ~100 line, GUI chat program.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Have a look at Building an Internet chat system.

This explains how to write simple Clients and a Server with Java.

Peter Lang
  • 54,264
  • 27
  • 148
  • 161
0

Unless you want to get into really complicated P2P discovery protocols, you would have to have a server to act at least as an intermediary.

In order to establish a direct client to client connection, the clients would need to know each others IP addresses. To do this, each client would first connect and "register" itself with a central server.

When a client wants to talk to another client, it would ask for that client's address from the server, then establish a connection directly with that client. So each client is acting both as a client (establishing connections with the server and other clients) and as a server (accepting connections from other clients).

It seems simple in theory, but in practice it gets more complicated. For example, what if the client you want to connect to is behind a firewall? You could have a hole in the firewall for incoming connections to get through, or you could fall back to having the communication go through the server, or if one of the clients is behind a firewall and the other isn't, the server could mediate the connection in the opposite direction.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
0

Basically, there are two approaches:

  1. One Chat server that receives all messages and distributes/forwards them to the clients (xmpp/jabber works this way)
  2. One server that connects clients directly. Like on peer-to-peer networks

Looking back at your previous work, I think, the first approach is more feasible.

The server will offer one port where new clients can connect. After a client requests to participate/use the server, there server spawns a worker thread with a server socket on a different (available) port number and tell the client that port number. This is the reserved communication channel for that client with the server.

The rest is pretty straightforward: a client can send a new chat message, the server will pick it up and send it to all connected clients.

If a client disconnects, the worker thread will close the socket, return it to the pool and terminate.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268