客户端
package com.cl.ws.a;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
/*
* Socket客户端,用来发送给服务端请求
*/
public class SocketClient {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
Socket sc = new Socket("127.0.0.1",8090);
//获取输出流,用来发送数据
System.out.println("请输入要转换的字母");
String initdata = input.next();
OutputStream out = sc.getOutputStream();
out.write(initdata.getBytes());
System.out.println("等待服务器端返回数据");
InputStream in= sc.getInputStream();
byte[] b = new byte[1024];
int len = in.read(b);
System.out.println("输出的结果是: "+new String(b,0,len));
in.close();
out.close();
sc.close();
}
}
服务端
package com.cl.ws.a;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
* Socket服务端,接受客户端的请求,实现小写转大写的功能
*/
public class SocketSer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8090);
while(true){
System.out.println("等待客户端请求.........");
Socket socket = ss.accept();
System.out.println("握手成功.........");
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);
OutputStream output = socket.getOutputStream();
output.write(data.toUpperCase().getBytes());
output.close();
input.close();
socket.close();
}
}
}
完成一个小写转大写的功能