欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于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();
	}
}


基于TCP协议的Client/Server网络编程基础
基于TCP协议的Client/Server网络编程基础
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();
	}
}


基于TCP协议的Client/Server网络编程基础
基于TCP协议的Client/Server网络编程基础