【发布时间】:2017-08-19 05:36:39
【问题描述】:
我正在编写一个 java 套接字程序,用于将 JSON 对象从客户端发送到另一个客户端。现在,我只是通过发送控制台输入的字符串数据来测试它。当我在控制台中输入内容并将其发送到服务器时,服务器应将其打印出来并将其发送回客户端,然后客户端也应将其打印出来。但是我的服务器程序在控制台中没有显示任何内容,客户端也没有。我找不到问题出在哪里。
服务器:
public class GameServer {
private Socket socket;
private ServerSocket serverSocket;
private ArrayList<GameServerThread> threads;
public GameServer(){
try{
//Create new server socket
serverSocket = new ServerSocket(1234,100);
System.out.println("Waiting for clients ...");
threads = new ArrayList<GameServerThread>();
//Receive request from client
while(true){
socket = serverSocket.accept();
//Create new Thread once receive a request
new Thread(new GameServerThread(socket, this)).start();
}
}catch(IOException e){
System.out.println("Failed to create socket!");
e.printStackTrace();
}finally{
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public ArrayList<GameServerThread> getAllThreads(){
return this.threads;
}
public void addNewThread(GameServerThread t){
this.threads.add(t);
}
public static void main(String args[]){
GameServer server = new GameServer();
}
}
服务器线程:
public class GameServerThread extends Thread{
private Socket socket;
private GameServer server;
private OutputStreamWriter writer;
private BufferedReader reader;
String userName;
public GameServerThread(Socket s, GameServer srv) throws UnsupportedEncodingException, IOException{
System.out.println("Succeeded to connect with one client!");
this.socket = s;
this.server = srv;
writer = new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8");
reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8"));
this.server.addNewThread(this);
}
public void run(){
try{
while(true){
receive();
}
}catch(SocketException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void receive() throws IOException{
String str = null;
while((str=reader.readLine()) != null){
System.out.println(str);
sendToOther(str); }
}
public void send(String msg){
try{
writer.write(msg);
writer.flush();
}catch(IOException e){
e.printStackTrace();
}
}
private void sendToOther(String msg){
for(GameServerThread thread: this.server.getAllThreads()){
//if(!thread.equals(this)){
thread.send(msg);
//}
}
}
客户:
public class GameClient {
private Socket socket;
private String serverIP;
private OutputStreamWriter writer;
private BufferedReader reader;
public GameClient(String host){
this.serverIP = host;
try{
//Connect to server
socket = new Socket(InetAddress.getByName(serverIP), 1234);
writer = new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8");
reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8"));
//Start a new thread for reading from server
new Thread(new GameClientThread(socket)).start();
Scanner scanner = new Scanner(System.in);
System.out.println("Write something: ");
String str = "";
while((str = scanner.nextLine()) != null){
writer.write(str);
writer.flush();
System.out.println("Write something: ");
}
}catch(IOException e){
System.out.println("Client failed to connect!");
e.printStackTrace();
}
}
客户端线程:
public class GameClientThread extends Thread{
private Socket socket;
private BufferedReader reader;
//private OutputStreamWriter writer;
//private String userName = "";
public GameClientThread(Socket soc){
this.socket = soc;
try{
reader = new BufferedReader(new InputStreamReader (this.socket.getInputStream(), "UTF-8"));
}catch(IOException e){
e.printStackTrace();
}
}
public void run(){
while(true){
receive();
}
}
private void receive(){
String msg;
try {
msg = reader.readLine();
System.out.println(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
对不起,这不是 StackOverflow 的工作方式。 “这是我的一堆代码,它不起作用,有人可以帮我弄清楚” 形式的问题被认为是题外话。请访问help center并阅读How to Ask了解更多信息,尤其是阅读Why is “Can someone help me?” not an actual question?
标签: java sockets server client