服务端:
import java.io.*;import java.net.*;
public class MyServer {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(5678);
Socket client=server.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
System.out.println(str);
out.println("has receive....");
out.flush();
if(str.equals("end"))
break;
}
client.close();
}
}
accept()方法说明:
public Socket accept() throws IOException
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.
A new Socket s is created and, if there is a security manager, the security manager's checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.
客户端
import java.net.*;import java.io.*;
public class Client{
static Socket server;
public static void main(String[] args)throws Exception{
server=new Socket(InetAddress.getLocalHost(),5678);
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter out=new PrintWriter(server.getOutputStream());
//标准输入
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
while(true){
String str=wt.readLine();
out.println(str);
out.flush();
if(str.equals("end")){
break;
}
System.out.println(in.readLine());
}
server.close();
}
}
一对多的服务端和客户机:一个服务端只能同时服务多个客户端,需要使用多线程来实现
服务端:调用具体的处理线程完成处理
import java.io.*;
import java.net.*;
public class MyMultiServer {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(5678);
while (true){
Socket client=server.accept();
ChildTh child=new ChildTh(client);//用socket实例初始化具体的处理线程对象
//使用Runnable接口和使用extends Thread的区别:
// 如果是使用Runnable接口,需要这么写:
new Thread(child).start();
//注意:不能写成:child.run();
// 如果使用extends Thread,只需要child.start()就可以,因为start()是Thread的方法
}
}
}
处理线程类:
import java.io.*;
import java.net.*;
public class ChildTh implements Runnable{
private Socket client;
public ChildTh(Socket client){
this.client=client;
}
public void run(){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
System.out.println(str);
out.println("has receive....");
out.flush();
if(str.equals("end"))
break;
}
client.close();
}catch(Exception e){
System.out.println("error in the close the socket!");
e.printStackTrace();
}
finally{
}
}
}
客户端:
import java.net.*;
import java.io.*;
public class Client{
static Socket server;
public static void main(String[] args)throws Exception{
server=new Socket(InetAddress.getLocalHost(),5678);
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter out=new PrintWriter(server.getOutputStream());
//标准输入
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
while(true){
String str=wt.readLine();
out.println(str);
out.flush();
if(str.equals("end")){
break;
}
System.out.println(in.readLine());
}
server.close();
}
}






















大小: 8KB
大小: 89.5M
大小: 304.3M
终端仿真器 SecureCRTv7.1.1.264 最新版
串口调试小助手1.3 免费版
WinHex 十六进制编辑器v20.2 SR-5 绿色中文注册版
小旋风ASP服务器安装版
16进制转换工具V1.0 中文绿色版
Adobe AIRV33.1.1.932 官方最新版
github离线安装包64位版V2.9.11官方最新版(github desktop)
MSDN Library Visual Studio 6.0(VC、VB、VF、VJ)中文版win32开发人员必备
版本控制软件(SourceTree)v3.4.6 官方最新版
Memory Analyzer (MAT)V1.01 免费绿色版
Delphi Distillerv1.85绿色版
IBM内存检测工具(IBM Thread and Monitor Dump Analyzer for Java)V4.3.3 绿色版
.NETv3.0 可再发行组件包
一键安装JSP环境安装版
slave4j(基于eclipse插件的java代码生成器)V1.0.0 正式版
Auto DebugProfessional 5.6.5.18 中文绿色版
Understand For C++V1.4.319英文安装版