基于TCP协议的Client/Server网络编程基础
程序员文章站
2022-06-14 10:18:25
...
1、 编写一个基于TCP协议的服务器程序。服务器等待客户端通报它的名字,服务器回复名字并加上“你好!”的问候。
//Server1.java
import java.net.*;
import java.io.*;
class Server1
{
public static void main(String[] args) throws IOException
{
ServerSocket svsk=new ServerSocket(10000);
System.out.println("我的专属服务器开始运行。"+new java.util.Date());
while(true)
{
Socket sk=svsk.accept();
InputStream in=sk.getInputStream();
OutputStream out=sk.getOutputStream();
InputStreamReader ir=new InputStreamReader(in);
BufferedReader br=new BufferedReader(ir);
OutputStreamWriter ow=new OutputStreamWriter(out);
BufferedWriter bw=new BufferedWriter(ow);
String s=br.readLine();
System.out.println(s+"来访。");
bw.write("你好!"+s);
bw.newLine();
bw.flush();
in.close();
out.close();
sk.close();
}
}
}
//Client1.java
import java.net.*;
import java.io.*;
class Client1
{
public static void main(String[] args) throws IOException
{
Socket sk=new Socket("127.0.0.1",10000);
InputStream in=sk.getInputStream();
OutputStream out=sk.getOutputStream();
InputStreamReader ir=new InputStreamReader(in);
BufferedReader br=new BufferedReader(ir);
OutputStreamWriter ow=new OutputStreamWriter(out);
BufferedWriter bw=new BufferedWriter(ow);
bw.write("你的名字");
bw.newLine();
bw.flush();
String s=br.readLine();
System.out.println(s);
in.close();
out.close();
sk.close();
}
}
2、 编写一个基于TCP协议的客户端程序。客户端程序连接上服务器程序后,通报自己的名字,等待服务器的回复问候。
//TCPServer.java
import java.io.*;
import java.net.*;
class TCPServer{
public static void main(String [] args) throws IOException{
ServerSocket ss = new ServerSocket(1000);
System.out.println("我的服务器"+new java.util.Date());
while(true){
Socket s=ss.accept();
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
InputStreamReader ir = new InputStreamReader(in);
OutputStreamWriter ow = new OutputStreamWriter(out);
BufferedWriter dout = new BufferedWriter(ow);
BufferedReader din = new BufferedReader(ir);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1,s2;
do{
s1=din.readLine();
System.out.println("Client Says:"+s1);
System.out.println("Server says:");
s2=br.readLine();
dout.write(s2);
dout.newLine();
dout.flush();
}while(!s1.equals("BYE"));
in.close();
out.close();
s.close();
}
}
}
//TCPClient.java
import java.net.*;
import java.io.*;
class TCPClient{
public static void main(String [] args) throws IOException{
Socket s = new Socket("127.0.0.1",1000);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
InputStreamReader ir = new InputStreamReader(in);
OutputStreamWriter ow = new OutputStreamWriter(out);
BufferedWriter dout = new BufferedWriter(ow);
BufferedReader din = new BufferedReader(ir);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1,s2;
do{
System.out.println("Client Says:");
s1=br.readLine();
dout.write(s1);
dout.newLine();
dout.flush();
s2=din.readLine();
System.out.println("Server says:"+s2);
}while(!s1.equals("BYE"));
in.close();
out.close();
s.close();
}
}
推荐阅读
-
Java编程实现基于TCP协议的Socket聊天室示例
-
Java编程实现基于TCP协议的Socket聊天室示例
-
C#基于TCP协议的服务器端和客户端通信编程的基础教程
-
使用C#实现基于TCP和UDP协议的网络通信程序的基本示例
-
python 之 网络编程(基于TCP协议的套接字通信操作)
-
基于TCP协议的套接字编程
-
Android基于TCP和URL协议的网络编程示例【附demo源码下载】
-
基于Java的Socket类Tcp网络编程实现实时聊天互动程序:QQ聊天界面的搭建
-
【Netty】Socket 编程(C/S):基于Netty的Server、Client示例(少注释)
-
基于TCP协议的网络通信