Java网络编程之单线程Client-Server

       最近在写这个计算机网络,然后要求我们自己去写一个服务器,一开始一头雾水!不过后来一查资料想通了,哈哈~然后自己就啪啪啪敲出来了。好啦,闲言少叙,直接看代码!

Client.java

package exercise01;
import java.io.*;
import java.net.*;

public class Client {
    private String hostname;
    private int port;
    Socket socket = null;
    
    public Client(String hostname, int port){
    	//constructor of the Client class
    	this.hostname = hostname;
    	this.port = port;
    }
    
    public void connect() throws UnknownHostException, IOException{
    	System.out.println("Attempting connect to "+ hostname +":"+port);
    	socket = new Socket(hostname,port);
    	System.out.println("Connection established!");
    }
    
    public void readResponse() throws IOException{
    	String userInput;
    	BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    	System.out.println("Response from the server:");
    	while((userInput = reader.readLine() )!= null ){
    		System.out.println(userInput);
    	}
    }
    
    public static void main(String[] argv){
    	//create an object for the current class Client
    	Client client = new Client("localhost",8181);
    	try{
    		//trying to establish a connection to the server
    		client.connect();
    		
    		//if connection succeed, return the input contents
    		client.readResponse();
    		
    	}catch(UnknownHostException ukhe){
    		//if the host not found
    		System.err.println("Host unknown! Connection can not be established!");
    	}catch(IOException ioe){
    		//if the server doesn't work
    		System.err.println("Connection can not be established! The server may not be on! Check the error message! "+ioe.getMessage());
    	}
    }
    
    
}

Server.java

package exercise01;

import java.net.*;
import java.io.*;

public class Server {
	private ServerSocket serverSocket;
	private int port;
	
	public Server(int port){
		this.port = port;
	}
	
	public void startServer() throws IOException{
		System.out.println("Starting the socket server at the port: " + port);
		serverSocket = new ServerSocket(port);
		
		//Listen the clients. Block until one connects
		System.out.println("Waiting for clients...");
		Socket client = serverSocket.accept();
		
		//A client has connected and send the welcome message
		sendMessage(client);
	}

	//send the display message
	private void sendMessage(Socket client) throws IOException {
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
		writer.write("Hello. You are connected to a Simple Socket Server.");
		writer.flush();
		writer.close();
	}
	
	/**
	 * Create a server object and start the server
	 * 
	 * @param argv
	 */
	public static void main(String[] argv){
		int port = 8181;
		
		//start the server
		try{
			Server server = new Server(port);
			server.startServer();
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
	}
}

自己将包名改一下就可以跑了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值