我正在编写一个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();
}
}
ServerThread:
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();
}
}
}
答案 0 :(得分:0)
BufferedReader.readLine需要在每一行的末尾添加一个新行(\ n,\ r \ n或\ r \ n \ n \ n),但在编写数据时不要写新行。每次编写一个尚未在新行中结束的字符串时,请添加writer.write('\n');
。