我的代码如下:
package heh;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
ServerSocket ss;
Socket s;
PrintWriter pw;
InputStreamReader isr;
BufferedReader br;
public MyServer(){
try {
ss = new ServerSocket(5555);
System.out.println("服务器端正在等待连接......");
s = ss.accept();
System.out.println("连接成功!!!");
pw = new PrintWriter(s.getOutputStream(),true);
isr = new InputStreamReader(s.getInputStream());
br = new BufferedReader(isr);
String name = br.readLine();
System.out.println(name);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new MyServer();
}
}
客户端代码:
package heh;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class MyClient {
Socket s;
PrintWriter pw;
InputStreamReader isr;
BufferedReader br;
public MyClient(){
try {
s = new Socket("127.0.0.1",5555);
pw = new PrintWriter(s.getOutputStream());
isr = new InputStreamReader(s.getInputStream());
br = new BufferedReader(isr);
pw.println("Doom");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new MyClient();
}
}
我先运行服务端 再运行客户端 这时候服务端会报错 求看程序什么地方错了?
我弄明白了,我没有刷新流。还没把信息写出去客户端程序就结束了