这段JAVA小程序错在了哪里???

来源:百度知道 编辑:UC知道 时间:2024/06/01 19:01:14
public class TestClient {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 2000);
String str;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s
.getOutputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(s
.getInputStream()));
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
while (!str.equalsIgnoreCase("bye")) {
bw.write(str);
bw.flush();
System.out.println("client:" + str);
System.out.println("server:" + br.readLine());
str = sc.nextLine();
}
br.close();
bw.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public class TestServer {
public static void main(S

给你段代码,你参考一下:
public class SimpleServer {
private ServerSocket ss = null;
private Socket s1;
private String sendString = "Hello Client!";
private OutputStream s1out;
private DataOutputStream dos;

public void toClient(){
try {
ss = new ServerSocket(123);
}catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
s1 = ss.accept();
s1out = s1.getOutputStream();
dos = new DataOutputStream(s1out);
dos.writeUTF("11111");
s1out.close();
s1.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
SimpleServer ss = new SimpleServer();
ss.toClient();
}
}

public class SimpleClient {
private Socket s1;
private InputStream s1In;
private DataInputStrea