-3

サーバーとクライアントを接続する簡単なアプリケーションを Java で作成しましたが動作しません

new Socket("localhost", 4444); を使用します。クライアントと新しい ServerSocket(4444) の場合。サーバー用

クライアント: http://www.hastebin.com/siqamonebu.coffee

サーバー: http://www.hastebin.com/rivebetani.avrasm

ここに投稿するには大きすぎるため、コードをhastebinに投稿しました。どの部分が壊れているのかわかりませんが、サーバーはあなたが言うメッセージを受け取りません

4

2 に答える 2

2

ChatServer - 接続されているすべてのクライアントにブロードキャストします

1 つのコマンド プロンプト:java ChartServer
別の場合: java ChatClient localhost (またはサーバーが実行されている場所の IP アドレス)
別の場合: java ChatClient localhost (またはサーバーが実行されている場所の IP アドレス)

クライアント ウィンドウでチャットを開始します。

このようなサーバー...

// xagyg wrote this, but you can copy it
import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {

    public static List list = new ArrayList();

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

        ServerSocket svr = new ServerSocket(4444);

        System.out.println("Chat Server started!");

        while (true) {
            try {
                Socket s = svr.accept();
                synchronized(list) {
                   list.add(s);              
                }                                  
                new Handler(s, list).start();
            }
            catch (IOException e) {
                // print out the error, but continue!
                System.err.println(e);
            }
        }
    }
}

class Handler extends Thread {

    private Socket s;
    private String ipaddress;
    private List list;

    Handler (Socket s, List list) throws Exception {
      this.s = s;
      ipaddress = s.getInetAddress().toString();
      this.list = list;
    }

    public void run () {

      try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String message;
        //MyDialog x = (MyDialog)map.get(ipaddress.substring(1));
        while ((message = reader.readLine()) != null) {
            if (message.equals("quit")) {
                synchronized(list) {
                    list.remove(s);
                }
                break;
            }
            synchronized(list) {
                for (Object object: list) {
                    Socket socket = (Socket)object;
                    if (socket==s) continue;
                    PrintWriter writer = new PrintWriter(socket.getOutputStream());
                    writer.println(ipaddress + ": " + message);
                    writer.flush();
                }
            }
        }
        try { reader.close(); } catch (Exception e) {}
      }
      catch (Exception e) {
        System.err.println(e);
      }
    }
}

こんなお客様...

// xagyg wrote this, but you can copy it
import java.util.*;
import java.io.*;
import java.net.*;

public class ChatClient {


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

        Socket s = new Socket(args[0], 4444);
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter out = new PrintWriter(s.getOutputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String message;
        new SocketReader(in).start();   
        while ((message = reader.readLine())!=null) {
            out.println(message);
            out.flush();
            if (message.equals("quit")) break;
        }
        in.close();
        out.close();
    }        
}

class SocketReader extends Thread {

    BufferedReader in;

    public SocketReader(BufferedReader in) {
        this.in = in;
    }

    public void run() {   
        String message;
        try {
            while ((message = in.readLine())!=null) {
                System.out.println(message);
            }
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }        
    }
}
于 2013-02-25T15:06:29.130 に答える
0

サーバーをどのように起動するか、この構造を試してみてください.runメソッド内でServerSocketを作成しているため、競合が発生する可能性があるためだと思います。

public class ThreadSocketServer implements Runnable {

    private int port;
    private boolean isRunning;
    private ServerSocket ss;

    public ThreadSocketServer(int port, boolean initialStatus) {
        this.port = port;
        isRunning = initialStatus;
        System.out.println("Start the server!");
        try {
            ss = new ServerSocket(port);
            Thread threadServer = new Thread(this);
            threadServer.start();
        } catch (IOException e) {
            System.err.println(e.getClass() + " - " + e.getMessage());
        }
    }

    @Override
    public void run() {

        while (isRunning) {
            try {
                Socket client = ss.accept();

                ObjectInputStream ois = new ObjectInputStream(
                        client.getInputStream());
                //Process here
                ois.close();
            } catch (IOException e) {
                System.err.println(e.getClass() + " - " + e.getMessage());
                isRunning = false;
            } catch (ClassNotFoundException e) {
                System.err.println(e.getClass() + " - " + e.getMessage());
                isRunning = false;
            }
        }

    }

    public static void main(String[] args) {
        new ThreadSocketServer(1085, true);
    }
于 2013-02-25T14:16:55.823 に答える