标签:new 输出 NPU oid 输入 sys exception 输入输出 字符
//客户端:
package com.gxnu.study.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
//建立客户端Socket连接,指定服务器的位置和端口
Socket socket = new Socket("192.168.224.12", 9999);
//打开输入输出流
OutputStream os = socket.getOutputStream();
String info = null;
/*
* System.in-->InputStream
* new InputStream(...)把字节流转化为字符流
*/
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while((info = br.readLine())!=null){
os.write((info+"\n").getBytes());
}
//关闭资源
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//服务器端
package com.gxnu.study.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try{
//建立一个服务器Socket(ServerSocket)指定端口并开始监听
ServerSocket serverSocket = new ServerSocket(9999);
//使用accept()方法等待客户端触发通信
Socket socket = serverSocket.accept();
//打开输入输出流
InputStream is = socket.getInputStream();
//获取客户端信息,即从输入流读取信息
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String info = null;
while(!((info=br.readLine())==null)){
System.out.println("服务器,客户信息为:"+info);
}
//关闭资源
br.close();
is.close();
socket.close();
serverSocket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
标签:new 输出 NPU oid 输入 sys exception 输入输出 字符
原文地址:https://www.cnblogs.com/jiminluo/p/9357319.html