Java Socket编程入门
一、服务器端创建ServerSocket对象
ServerSocket(int Port);
ServerSocket s=new ServerSocket(int port,int backlog);分别用于指明端口跟最大连接数,默认为50;
s.accept() 返回一个Socket对象
Socket 对象:
Socket ctc=new Socket(InetAddress address,int port);
二、输入、输出流
1、DataInputStream in=new DataInputStream(ctc.getInputStream());//用于接受数据,
String str=in.readUTF();
DataOutputStream out=new DataOutputStream(ctc.getOutputStream());//用于发送数据
out.writeUTF(str);
out.flush();
2、BufferedReader read=new BufferedReader(new InputStreamReader(ctc.getInputStream()));//输入流
String msg =read.readLine();
PrintWriter write=new PrintWriter(ctc.getOutputStream(),true);//输出流
write.println(msg);