刚做了个简单的TCP
先说下需求吧:
//通过客户端键盘录入单词,并发送到服务端。//服务端在控制台打印后,将该单词转成大写再发回客户端。
检查了好多遍了 不知哪里出错 】
导致客户端输入的不能在服务端转成大写还给客户端
public class ClientDemo {
public static void main(String[] args) throws Exception, IOException {
// 创建Socket对象 给服务端的ip 和 端口号
Socket socket = new Socket("10.1.31.66", 10123);
// 获取输入流
InputStream in = socket.getInputStream();
byte[] buffer = new byte[1024];
int a = in.read(buffer);
System.out.println(new String(buffer,0,a));
// 获取输出流
OutputStream out = socket.getOutputStream();
InputStream i = System.in;
byte[] buff = new byte[1024];
int b = i.read(buff);
out.write(buff, 0, b);
}
}
public class ServerDemo {
public static void main(String[] args) throws Exception {
// 创建TCP的端口并监听
ServerSocket server = new ServerSocket(10123);
// 获得客服对象
Socket socket1 = server.accept();
// 获得客服对象ip
String ip = server.getInetAddress().getHostAddress();
System.out.println(ip + "-------ing");
// 获得对象的输入流
while(true) {
InputStream in = socket1.getInputStream();
byte[]buffer = new byte[1024];
int b = in.read(buffer);
System.out.println(new String(buffer,0,b));
// 获得对象的输出流
OutputStream out = socket1.getOutputStream();
byte[]buff = (new String(buffer,0,b).toUpperCase()).getBytes();
out.write(buffer);
}
}
}
|
|