package Net.TCP.Demo2;
import java.io.*;
import java.net.Socket;
public class SendDemo {
public static void main(String[] args) throws IOException {
//创建Socket对象
Socket s=new Socket("DESKTOP-QG1LSJ2",12000);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名");
String s1 = br.readLine();
System.out.println("请输入密码");
String s2 = br.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(s1);
pw.println(s2);
//
BufferedReader chickbr=new BufferedReader(new InputStreamReader(s.getInputStream()));
String flag=chickbr.readLine();
System.out.println(flag);
s.close();
}
}
删除线格式
package Net.TCP.Demo2;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**模拟用户登陆*/
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(12000);
Socket s = ss.accept();
// InputStream is = s.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String usename=br.readLine();
String message=br.readLine();
System.out.println(usename);
System.out.println(message);
boolean flag=false;
if("wujinchao".equals(usename)&&"10000".equals(message)){
flag=true;
}
// BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
if (flag){
pw.println("登陆成功");
}
else {
pw.println("登陆失败");
}
s.close();
//ss.close //服务器一般不关闭
}
}