网络编程之TCP协议
TCP协议概念
TCP通信需要经过创建连接、数据传送、终止连接三个步骤。 TCP通信模型中,在通信开始之前,一定要先建立相关的链接,才能发送数据,类似于生活中,“打电话”
TCP 客户端和服务器端实现
public static void main(String[] args) throws IOException {
System.out.println("----我是客户端---------");
Socket client = new Socket("127.0.0.1", 8989);
DataOutputStream os = new DataOutputStream(client.getOutputStream());
os.writeUTF("哈哈");
os.flush();
os.close();
client.close();
}
----------------------------------------------------------------------------------------
public static void main(String[] args) throws IOException {
System.out.println("----我是服务端---------");
ServerSocket server = new ServerSocket(8989);
Socket client = server.accept();
System.out.println("----一个客户端连接成功---------");
DataInputStream is = new DataInputStream(client.getInputStream());
String str = is.readUTF();
System.out.println(str);
is.close();
client.close();
server.close();
}
练习TCP【输入用户名密码,服务端判断对错】
public static void main(String[] args) throws IOException {
System.out.println("----我是客户端---------");
Socket client = new Socket("127.0.0.1", 8989);
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名 : ");
String username = rd.readLine();
System.out.println("请输入用户密码 : ");
String password = rd.readLine();
DataOutputStream os = new DataOutputStream(client.getOutputStream());
os.writeUTF("username="+username+"&password="+password);
os.flush();
os.close();
rd.close();
client.close();
}
-----------------------------------------------------------------------------------------------
public static void main(String[] args) throws IOException {
System.out.println("----我是服务端---------");
ServerSocket server = new ServerSocket(8989);
Socket client = server.accept();
System.out.println("----一个客户端连接成功---------");
DataInputStream is = new DataInputStream(client.getInputStream());
String str = is.readUTF();
String msg = "username="+"zhangsan"+"&password="+"1234";
if(msg.equals(str)){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
is.close();
client.close();
server.close();
}
tcp协议实现双向通信
public static void main(String[] args) throws IOException {
System.out.println("----我是客户端---------");
Socket client = new Socket("127.0.0.1", 8989);
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名 : ");
String username = rd.readLine();
System.out.println("请输入用户密码 : ");
String password = rd.readLine();
DataOutputStream os = new DataOutputStream(client.getOutputStream());
os.writeUTF("username="+username+"&password="+password);
os.flush();
DataInputStream is = new DataInputStream(client.getInputStream());
String result = is.readUTF();
System.out.println(result);
is.close();
os.close();
rd.close();
client.close();
}
-------------------------------------------------------------------------------------
public static void main(String[] args) throws IOException {
System.out.println("----我是服务端---------");
ServerSocket server = new ServerSocket(8989);
Socket client = server.accept();
System.out.println("----一个客户端连接成功---------");
DataInputStream is = new DataInputStream(client.getInputStream());
String str = is.readUTF();
String uname = "";
String upwd = "";
String[] arr = str.split("&");
for(String s:arr){
String[] arr2 = s.split("=");
if("username".equals(arr2[0])){
uname = arr2[1];
}else if("password".equals(arr2[0])){
upwd = arr2[1];
}
}
DataOutputStream os = new DataOutputStream(client.getOutputStream());
if("zhangsan".equals(uname) && "1234".equals(upwd)){
os.writeUTF("登录成功");
}else{
os.writeUTF("登录失败");
}
os.flush();
os.close();
is.close();
client.close();
server.close();
}
|