Java Socket和ServerSocket无法通信

时间:2018-02-12 10:41:59

标签: java sockets tcp

自我解释,服务器处理客户端并识别连接,但他们还无法进行通信。这是代码:

服务器类:

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

public class Server{
    private final int PORT=8080;
    public BufferedReader fromClient;
    public PrintWriter toClient;
    public ServerSocket server;
    public Socket clientSocketHandler;
    public Server(){ //constructor

        if(startServer())
            System.out.println("Server Avviato...");
        else
            System.out.println("Errore nell'avvio del server...");
    }

    private boolean startServer(){
        try{
            server = new ServerSocket(PORT);
        }catch(IOException e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public void runServer(){
        try{
            System.out.println("Server in ascolto sulla porta " + PORT + " ...");
            clientSocketHandler = server.accept();
            System.out.println("Client Connesso...");

            InputStreamReader isr = new InputStreamReader(clientSocketHandler.getInputStream());
            fromClient = new BufferedReader(isr);

            OutputStreamWriter osr = new OutputStreamWriter(clientSocketHandler.getOutputStream());
            BufferedWriter bw = new BufferedWriter(osr);
            toClient = new PrintWriter(bw);


        }catch(IOException e){
            e.printStackTrace();
        }
    }

}

MainServer类:

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

public class MainServer{

    public static void main(String args[]){
        Server s1 = new Server();
        s1.runServer();

        float n1,n2;
        String op;

        try{
            s1.toClient.println("comunicazione da server");
            System.out.println("Aspettando i numeri...");
            n1=Float.parseFloat(s1.fromClient.readLine());
            n2=Float.parseFloat(s1.fromClient.readLine());
            System.out.println("Numeri accettati");

            System.out.println("Aspettando operatore...");
            op=s1.fromClient.readLine();
            System.out.println("Operatore accettato");
        }catch(IOException e){
            e.printStackTrace();
        }
    }



}

客户端类:

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

public class Client{
    private final int PORT=8080;
    private InetAddress address = InetAddress.getLoopbackAddress();
    public PrintWriter toServer;
    public BufferedReader fromServer;
    public BufferedWriter bw;
    public Socket client;

    public Client(){
        if(startClient())
            System.out.println("Connesso al Server...");
        else
            System.out.println("Errore nella connessione...");
    }

    private boolean startClient(){
        try{
            client = new Socket(address, PORT); 

            OutputStreamWriter osw = new OutputStreamWriter(client.getOutputStream());
            bw = new BufferedWriter(osw);
            toServer = new PrintWriter(bw);

            InputStreamReader isr = new InputStreamReader(client.getInputStream());
            fromServer = new BufferedReader(isr);
        }catch(IOException e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

MainClient类:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class MainClient{
    public static void main(String args[]){

        Client c = new Client();

        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader kBoard = new BufferedReader(isr);

        String n1,n2,op;

        try{
            System.out.println(c.fromServer.readLine());
            System.out.println("Inserisci il primo numero");
            n1=kBoard.readLine();

            System.out.println("Inserisci il Secondo numero");
            n2=kBoard.readLine();

            System.out.println("Passando i valori al server");
            c.toServer.println(n1);
            c.toServer.println(n2);

            System.out.println("Inserisci l'operatore [+,-,/,*]");
            op = kBoard.readLine();

            System.out.println("Passando i valori al server");
            c.toServer.println(op);
        }catch(IOException e){
            e.printStackTrace();
        }



    }
}

抱歉意大利语字符串。 它的客户端被困在Connesso al Server ......"和服务器卡在" Aspettando i numeri"

救救我!! :)

1 个答案:

答案 0 :(得分:0)

  • 您应该在服务器套接字方法while(true) {...}中进行无限循环(runServer)以接受客户端套接字。没有它,就听不到!
  • 为了处理多个客户端,您还应该进行多线程处理;即每次接受新客户端时,都会启动一个新线程来处理请求。

应该帮助你。祝你有美好的一天......