car
2018-06-29 22:00:36浏览 3005
import java.net.*;
import java.io.*;
public class GreetingServer {
public static void main(String[] args) throws IOException {
int port = 6066;
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
OutputStream os = socket.getOutputStream();
String info;
while ((info = br.readLine()) != null) {
System.out.println("客户端说:" + info);
info=info+"\n";
os.write(info.getBytes("utf-8"));
}
PrintWriter pw = new PrintWriter(os);
pw.write("欢迎您\n");
pw.flush();
pw.close();
isr.close();
is.close();
br.close();
socket.close();
serverSocket.close();
}
}
import java.net.*;
import java.io.*;
public class GreetingClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 6066);
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.write("用户名:admin;密码123\n");
pw.flush();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String info;
while ((info = br.readLine()) != null) {
System.out.println("服务端说:" + info);
}
pw.close();
br.close();
isr.close();
is.close();
socket.close();
}
}