版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014252478/article/details/84591333
websocket 客户端:
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
/*
* 客户端用来发给服务端请求
*
* */
public class client {
public static void main(String[] args) throws Exception{
Scanner input = new Scanner(System.in);
//1、创建一个基于TCP协议的socket服务,在建立对象时,要指定连接服务器和端口号
Socket sc = new Socket("127.0.0.1", 9999);
//2、通过建立的socket对象获取socket中的输出流,调用getOutStream方法
OutputStream out = sc.getOutputStream();
System.out.println("输入要转化的字母 :");
String initData = input.next();//获取控制台的输入
//3、写入到socket输出流中
out.write(initData.getBytes());
System.out.println("等待服务器端返回数据");
//4、通过建立socket对象获取socket中的输入流,输入流会接受来自服务器端数据
InputStream in = sc.getInputStream();
byte[] b = new byte[1024];
//5、获取输入字节流的数据,注意此方法是堵塞的,如果没有获取数据会一直等待
int len = in.read(b);
System.out.println("返回结果为: " + new String(b, 0, len));
//关闭socket
out.close();
in.close();
sc.close();
input.close();
}
}
websocket 服务端:
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
* Socket服务端,用来接收客户端请求,实现转大写功能
*
* */
public class server {
public static void main(String[] args) throws Exception{
//1、建立服务器端的tcp socket服务, 必须监听一个端口
ServerSocket ss = new ServerSocket(9999);
while(true){
System.out.println("等待客户端请求.....");
//2、通过服务器端的socket对象的accept方法获取连接上的客户端de对象,没有则堵塞
Socket socket = ss.accept();
//3、通过输入流获取数据
InputStream input = socket.getInputStream();
byte[] b = new byte[1024];
int len = input.read(b);
String data = new String(b, 0, len);
System.out.println("客户端数据为:" + data);
//4、通过服务器socket输出流,写数据,会传到客户端socket输入流中
OutputStream output = socket.getOutputStream();
output.write(data.toUpperCase().getBytes());
//5、关闭socket
output.close();
input.close();
socket.close();
}
}
}