作者:雨后_1249677911
来源:CSDN
原文:https://blog.csdn.net/qq_30387171/article/details/80312390
版权声明:本文为博主原创文章,转载请附上博文链接!
服务器端
服务器类
public class ServerDemo01 {
public static void main(String[] args) {
/*
* 服务器端测试
* public ServerSocket(int port) throws IOException {
this(port, 50, null);//默认允许接入50个客户端 绑定本地的端口 即127.0.0.1
}
* */
try {
//创建一个服务器端的Socket
ServerSocket serversocket=new ServerSocket(9999);//端口号范围 0-65535 建议使用尽量大的端口号
//等待客户端的连接 accept()方法是阻塞式的方法 只有客户端连接进来的时候才会执行
Socket socket =null;
System.out.println("服务即将启动");
//记录由多少个客户端连接进来
int count=0;
while(true) {//不断循环检测是否有客户端连接进来
socket = serversocket.accept();//alt+shift+l快捷键自动接收
//若有一个客户端连接进来 则启动一个线程与这个客户端进行通信
System.out.println("一个客户端连接成功");
Thread thread=new Thread(new ServerThread(socket));
thread.start();
count++;
System.out.println("服务器被连接过的次数:"+count);
InetAddress address=socket.getInetAddress();
System.out.println("当前客户端的IP为:"+address);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
线程处理类
public class ServerThread implements Runnable {
Socket socket=null;//与本线程有关的Socket
public ServerThread(Socket socket){
this.socket=socket;
}
public void run(){
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
OutputStream os=null;
PrintWriter pw=null;
try{
//与客户端建立通信,获取输入流,获取客户端提供的信息
is=socket.getInputStream();
isr=new InputStreamReader(is);
br=new BufferedReader(isr);
//获取输出流,响应客户端的请求
os=socket.getOutputStream();
pw=new PrintWriter(os);
String data=null;
while((data=br.readLine())!=null){
//循环获取客户端的信息
System.out.println("我是服务器,客户端提交的信息为:"+data);
pw.write("服务器端响应成功");
pw.flush();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(pw!=null)
pw.close();
if(os!=null)
os.close();
if(br!=null)
br.close();
if(isr!=null)
isr.close();
if(is!=null)
is.close();
if(socket!=null)
socket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
客户端
public class ClientDemo01 {
public static void main(String[] args) {
/*
* 创建客户端的socket
* public Socket(String host, int port)
* host指服务器的IP地址 port指所要连接的端口号
* */
try {
String ip="10.13.34.39";
int port=9999;
Socket socket=new Socket(ip,port);
if(socket!=null)
System.out.println("客户端连接成功");
//获得socket的输出流
OutputStream os = socket.getOutputStream();
PrintStream ps=new PrintStream(os);
//获得socket的输入流 获得服务器端的响应
InputStream is=socket.getInputStream();
InputStreamReader isrs=new InputStreamReader(is);
BufferedReader brs=new BufferedReader(isrs);
//获取命令行输入流 并将其转换为字符流
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String line=null;
String server_message = null;
while((line=br.readLine())!=null) {
/*System.setOut(ps);//将命令行输入的内容输出内容更改到socket输出流中
System.out.println(line);
与下面的方法等效
*/
ps.println(line);
ps.flush();
String sm=brs.readLine();
System.out.println(sm);
/*
* String sm=brs.readLine();
* System.out.println(sm);
* 想每次发送过去再打印出服务器返回的信息,但是没有成功,不知道什么原因,希望有大神可以指教
*/
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
网友评论