Socket简单Demo
程序员文章站
2023-04-05 10:50:02
Socket协议网上介绍的有很多了,就不在画蛇添足了,本文主要编写一个小Demo,介绍下它具体实现 一:Socket服务器端 需要注意的是无论是客户端与服务端,执行write()方法后最好执行socket.shutdownOutput()方法关闭输出流,不然有几率写入不成功(原因未知) 我是分割线 ......
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服务端接收到请求后,会直接开启一个新线程执行任务