java初学者,代码出了问题不知道错在哪里,求指教!急!

2024-11-25 15:53:26
推荐回答(1个)
回答1:

问题1:打印流请用PrintStream,即,把PrintWriter改为PrintStream.
问题2:BufferedReader的readLine方法是阻塞等待的,而socket也是阻塞的,你一方发了消息,另一方的socket先要接收处理消息,才能使用readLine输入,要不就会造成阻塞等待,导致死锁。
所以你对方即收不到你的消息,也无法进行输入操作。
我帮你写了个完整的socket通信例子,你可以自己学习参考下:
服务端:
import java.io.*;
import java.net.*;
public class Server
{
public static final int PORT = 10086;
public static void main(String[] args){

try{

startServer();

}catch(Exception e){

e.printStackTrace();
}

}

public static void startServer() throws Exception{

ServerSocket server = new ServerSocket(PORT);
System.out.println("服务器已启动,等待客户端的连接...");

Socket client = null;
boolean connect = true;
int i = 1;
while (connect)
{
client = server.accept();

String clientName = "client-" + (i++);
new Thread(new MultiThreadServer(client,clientName)).start();
}
server.close();

}
}
客户端:
import java.io.*;
import java.net.*;

public class Client
{
public static final String IPADDRESS = "127.0.0.1";
public static final int PORT = 10086;
public static void main(String[] args){
try{

startClient();
}catch(Exception e){
e.printStackTrace();
}
}
public static void startClient() throws Exception{
Socket client = new Socket(IPADDRESS,PORT);
PrintStream out = new PrintStream(client.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
BufferedReader read = new BufferedReader(new InputStreamReader(client.getInputStream()));
boolean sent = true;
while (sent)
{
System.out.print("说点什么呢:");
String msg = input.readLine();
out.println(msg);
if("".equals(msg)){
sent = false;
}else{
if("bye".equals(msg)){

sent = false;
}else{
msg = read.readLine();
System.out.println(msg);

}

}
}
out.close();
client.close();

}
}
多线程(即可同时处理多个客户端的消息):
import java.io.*;
import java.net.*;
import java.util.*;
public class MultiThreadServer implements Runnable
{
private Socket client;
private String clientName;

private List list = new ArrayList();

public MultiThreadServer(Socket client,String clientName){
this.client = client;
this.clientName = clientName;

}

public void run(){

try{

messageHandle();

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

}

}

public void messageHandle() throws Exception{
System.out.println(clientName + "已连接上...");

PrintStream out = new PrintStream(client.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
BufferedReader read = new BufferedReader(new InputStreamReader(client.getInputStream()));
list.add(out);
boolean connect = true;
while(connect){

String msg = read.readLine();
if("".equals(msg)){
connect = false;

}else{

System.out.println(clientName + " : " + msg);
if("bye".equals(msg)){
connect = false;

}else{

//msg = read.readLine();
//System.out.println(msg);
for(PrintStream out1 : list){
out1.println("the server reply : " + msg);

}

}

}

}
out.close();
client.close();

}

public Socket getClient(){

return this.client;
}
public void setClient(Socket client){
this.client = client;

}
public String getClientName(){

return this.clientName;
}
public void setClientName(String clientName){
this.clientName = clientName;

}
public List getList(){
return this.list;
}
public void setList(List list){

this.list = list;
}
}
你可以运行看看,有问题欢迎提问!满意请采纳吧!