Pregunta

I've been working on this for a while now and I've hit a bit of a brick wall. I've tried googling lots but everything on the topic is either non server based or about using an API. I want to stick to the basic use of the java packages.

What I'm aiming for is two clients to be able to send messages back and forth, just in the console inside Eclipse would do for now. I know I'll have to implement threads in the server to handle the two connections and probably have to write a protocol too for the server.

Can anyone give me any pointers on this? Help greatly appreciated

Here's what I have so far

Client

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

public class Client 
{
static Socket client;
static String input;
static DataInputStream in;
static DataOutputStream out;
static BufferedReader keyboard;


//constructor, instantiation.
static void Open()
{
    try 
    {
        client = new Socket("127.0.0.1", 6666);
        InputStream sin = client.getInputStream();
        OutputStream sout = client.getOutputStream();

        in = new DataInputStream(sin);
        out = new DataOutputStream(sout);
    } 
    catch (UnknownHostException e) {
        System.err.println("Don't know about host: taranis.");
        System.exit(1);
    } 
    catch (IOException e) {
        System.err.println("Couldn't get I/O for " + "the connection to: Host.");
        System.exit(1);
    }

    keyboard = new BufferedReader(new InputStreamReader(System.in));

}

//close
static void Close() throws IOException
{
    client.close();
    keyboard.close();
    in.close();
    out.close();
}



public static void main(String[] args) throws IOException 
{

    Open();

    while(true) 
    {
        // wait for the user to type in something and press enter.
        input = keyboard.readLine(); 


        // send the above line to the server.
        out.writeUTF(input); 
        out.flush(); 

        // wait for the server to send a line of text.
        input = in.readUTF(); 

        System.out.println("The server was very polite. It sent me this : " + input);
        System.out.println();
    }


 }

 }

Host

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

public class Host 
{

//static variables
static ServerSocket host;
static Socket client;
static HostProtocol hp;
static String input1, output2;
static InputStream sin;
static OutputStream sout;
static DataInputStream in;
static DataOutputStream out;

//constructor
public Host() throws IOException
{
    hp = new HostProtocol();
    Open();

     sin = client.getInputStream();
     sout = client.getOutputStream();
     in = new DataInputStream(sin);
     out = new DataOutputStream(sout);
}

//open sockets, instantiate variables
public static void Open()
{
    try 
    {
        host = new ServerSocket(6666);
    } 
    catch (IOException e) 
    {
        System.err.println("Could not listen on port: 6666.");
        System.exit(1);
    }

    try 
    {
      client = host.accept();
    } 
    catch (IOException e) 
    {
        System.err.println("Accept failed.");
        System.exit(1);
    }
}

//close method
public static void Close () throws IOException
{
    out.close();
    client.close();
    host.close();
}


//main method
public static void main(String[] args) throws IOException 
{

    Host serverhost = new Host();
    input1=in.readUTF();

}
} 
¿Fue útil?

Solución

Your client will also need at least one extra thread so that it can receive and send chat messages at any time.

On the server side, you will need to put the call to accept inside a loop in order to accept multiple connections. Then, for each newly connected client you can spawn a new thread and pass it the socket object obtained from accept:

private class ClientThread implements Runnable {
    private Socket socket;

    public ClientThread(Socket socket) {
       this.socket = socket;
    }

    public void run() {
        // read messages from socket and send them to the other client.
    }
}

...

while(true) {
    try {
        client = host.accept();
        Thread t = new Thread(new ClientThread(client));
        t.start();
    } catch (IOException e) {
        System.err.println("Accept failed.");
        System.exit(1);
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top