一个java网络编程的问题
我的代码如下:packageheh;importjava.io.BufferedReader;importjava.io.IOException;importjava.i...
我的代码如下:
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();
}
}
我先运行服务端 再运行客户端 这时候服务端会报错 求看程序什么地方错了? 展开
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();
}
}
我先运行服务端 再运行客户端 这时候服务端会报错 求看程序什么地方错了? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询