问题:
客户端向服务端发送用户名请求登陆,服务端通过验证,返回“欢迎光临”,未通过“用户不存在”。
客户端:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class client
{
public static void send() throws IOException
{
Socket s = new Socket("127.0.0.1", 10000);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
DataInputStream in = new DataInputStream (s.getInputStream());
out.writeUTF((new Scanner(System.in)).nextLine());
System.out.println(in.readUTF());
in.close();
out.close();
s.close();
}
public static void main(String[] args) throws IOException
{
send();
}
}
服务端:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class server
{
public static void receive() throws IOException
{
ServerSocket now = new ServerSocket(10000);
Socket s = now.accept();
DataOutputStream out = new DataOutputStream(s.getOutputStream());
DataInputStream in = new DataInputStream (s.getInputStream());
String info = in.readUTF();
if (info.compareTo("jin") == 0)
out.writeUTF("欢迎光临");
else
out.writeUTF("用户不存在");
in.close();
now.close();
}
public static void main(String[] args) throws IOException
{
receive();
}
}
实验结果: