Socket简单Demo

2019-10-16 08:04:17来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Socket简单Demo

Socket协议网上介绍的有很多了,就不在画蛇添足了,本文主要编写一个小Demo,介绍下它具体实现

一:Socket服务器端

 

 

package com.founderit;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class HelloServer {

public static void main(String[] args) {
InputStream in = null;
OutputStream out = null;
try {
ServerSocket serverSocket= new ServerSocket(1234);
while (true){
Socket socket= serverSocket.accept();
in=socket.getInputStream();
InputStreamReader reader= new InputStreamReader(in);
StringBuilder builder=new StringBuilder();
for (int c = reader.read(); c != -1; c = reader.read()) {
builder.append((char)c);
}
System.out.println("收到的客户端请求为:-------"+builder.toString());
out=socket.getOutputStream();
out.write("请求已接收,over".getBytes());
out.flush();
socket.shutdownOutput();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in.close();
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}

二:Socket客户端

 

 

 

 

package com.founderit.controller;

import java.io.*;
import java.net.Socket;

public class SocketClient {
public static void main(String[] args) {
OutputStream out=null;
InputStream in=null;
Socket socket=null;
try {
socket=new Socket("localhost",1234);
out=socket.getOutputStream();
out.write("呼叫服务器,收到请回答".getBytes());
socket.shutdownOutput();
//获取输入流,并读取服务器端的响应信息
in=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String info=null;
while ((info=br.readLine())!=null){
sb.append(info);
}
System.out.println("收到服务端回复:"+sb.toString());
br.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.flush();
out.close();
in.close();
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}

}

首先执行Socket服务端,它会一直运行等待接收请求,创建socketServer需要绑定端口,这个端口需需要与客户端一致
然后执行Socket客户端,它会发送Socket请求给服务端并接收服务器端反馈,运行效果如下

 

 

 

 

 需要注意的是无论是客户端与服务端,执行write()方法后最好执行socket.shutdownOutput()方法关闭输出流,不然有几率写入不成功(原因未知)

 

----------------------------------------------------------------------------------------------------------------------我是分割线---------------------------------------------------------------------------------------------------------------------------------------------上面的Demo只是个单机版,在实际工作中不会只有一个客户端请求,下面我们将改造Socket服务器端,使其支持多线程

Socket服务器端

首先抽调具体实现,新建个具体实现类

 

 

 

package com.founderit;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;

public class HelloRunable implements Runnable{
Socket socket;
InputStream in = null;
OutputStream out = null;
public HelloRunable(Socket clientSocket){
this.socket=clientSocket;
}
@Override
public void run() {
try {
in=socket.getInputStream();
InputStreamReader reader= new InputStreamReader(in);
StringBuilder builder=new StringBuilder();
for (int c = reader.read(); c != -1; c = reader.read()) {
builder.append((char)c);
}
System.out.println("收到的客户端请求为:-------"+ new Date().toString()+ builder.toString());
out=socket.getOutputStream();
out.write("请求已接收,over".getBytes());
out.flush();
socket.shutdownOutput();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
in.close();
out.flush();
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
接收类

 

 

package com.founderit;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class HelloServer {

public static void main(String[] args) {

try {
ServerSocket serverSocket= new ServerSocket(1234);
while (true){
Socket socket=serverSocket.accept();
new Thread(new HelloRunable(socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
改造后,Socket服务端接收到请求后,会直接开启一个新线程执行任务




原文链接:https://www.cnblogs.com/lovetq520/p/11671187.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Maven仓库介绍以及私服搭建

下一篇:6 种微服务 RPC 框架,你知道几个?