黑马程序员技术交流社区
标题:
网络编程问题
[打印本页]
作者:
史小兵
时间:
2012-10-22 21:08
标题:
网络编程问题
这是Server端
import java.net.*;
import java.io.*;
public class Server {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss = null;
BufferedReader br=null;
PrintWriter pw=null;
BufferedReader bri=null;
try {
ss=new ServerSocket(4332);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Socket s = null;
try {
s=ss.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pw=new PrintWriter(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bri=new BufferedReader(new InputStreamReader(System.in));
String line = null;
// System.out.println("client1 shuo:"+br.readLine());
try {
line=bri.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(!line.equals("bye")){
pw.println(line);
pw.flush();
System.out.println("server1 shuo:"+line);
try {
System.out.println("client1 shuo:"+br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
line=bri.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pw.close();
try {
bri.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是Client端
import java.net.*;
import java.io.*;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Socket s=null;
BufferedReader br = null;
PrintWriter pw=null;
BufferedReader bri=null;
try {
s=new Socket("127.0.0.1",4332);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pw=new PrintWriter(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bri=new BufferedReader(new InputStreamReader(System.in));
String line = null ;
try {
line=bri.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(!line.equals("bye")){
pw.write(line);
pw.flush();
System.out.println("Client shuo"+line);
try {
System.out.println("Server shuo"+br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
line=bri.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pw.close();
try {
bri.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
为什么Client端发送过来的数据Server端收不到?
还有就是Server端不能输入?
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2