java ServerSocket and Socket programming


Server side

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

public class RemoteFileServer {
	protected int listenPort = 3000;
	protected static int times = 0;

	public static void main(String[] args) {
		RemoteFileServer server = new RemoteFileServer();
		server.acceptConnections();
	}

	public void acceptConnections() {
		try
		{
			ServerSocket server = new ServerSocket(listenPort);
			Socket incomingConn = null;
			while (true)
			{
				incomingConn = server.accept();
				handleConnection(incomingConn);
			}
		}
		catch (BindException e) {
			System.out.println("Unable to bind to port: " + listenPort);
		}
		catch (IOException e)
		{
			System.out.println("Unable to instantiate a ServerSocket  on port: " + listenPort);
		}
	}

	public void handleConnection(Socket incomingConnection) {
		try
		{
			OutputStream outputToSocket = incomingConnection.getOutputStream();
			InputStream inputFromSocket = incomingConnection.getInputStream();
			
			// we need 1. to read from target file, 2. to send to the client line by line
			BufferedReader streamReader = new BufferedReader(
				new InputStreamReader(inputFromSocket));
			// to read from a file we need a FileReader, then we need a File, we need a fileName
			// the fileName is input from Client side
			FileReader fileReader = new FileReader(new File(streamReader.readLine()));
			BufferedReader bufferedFileReader = new BufferedReader(fileReader);

			PrintWriter streamWriter = new PrintWriter(outputToSocket);
			String line = null;
			while ((line=bufferedFileReader.readLine()) != null)
			{
				streamWriter.println(line);
			}
			System.out.println("Handled " + ++times + (times > 1 ? " requests." : " request."));
			fileReader.close();
			streamWriter.close();
			streamReader.close();
		}
		catch (IOException e)
		{
			System.out.println("Error handling a client: " + e);
		}
	}
}

Client side.

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

public class RemoteFileClient {
	protected String hostIP;
	protected int hostPort;
	protected BufferedReader socketReader;
	protected PrintWriter socketWriter;

	public RemoteFileClient(String _hostIP, int _hostPort) {
		hostIP = _hostIP;
		hostPort = _hostPort;
	}

	public static void main(String args[]) {
		RemoteFileClient client = new RemoteFileClient("127.0.0.1", 3000);
		client.setUpConnection();
		//String doc = client.getFile("c:\\users\\leo\\setwifi.bat");
		String doc = client.getFile("c:\\users\\leo\\ftp\\learning.hub");
		client.tearDownConnection();
		System.out.println(doc);
	}

	// setting up a connection
	public void setUpConnection() {
		try
		{
			Socket socket = new Socket(hostIP, hostPort);
			socketReader = new BufferedReader(
				new InputStreamReader(socket.getInputStream()));
			socketWriter = new PrintWriter(socket.getOutputStream());
		}
		catch (UnknownHostException e) {
			System.out.println("Error settting up socket connection: unknow host at " 
				+ hostIP + ":" + hostPort);
		}
		catch (IOException e)
		{
			System.out.println("Error setting up socket connection: " + e);
		}
	}

	// talking to the host
	public String getFile(String fileNameToGet) {
		StringBuffer file = new StringBuffer();
		try
		{
			// sending request
			socketWriter.println(fileNameToGet);
			socketWriter.flush();

			String line = null;
			int lineNum = 0;
			while ((line=socketReader.readLine()) != null)
			{
				lineNum++;
				file.append(lineNum + " " + (line + "\n"));
			}
		}
		catch (IOException e)
		{
			System.out.println("Error reading file: " + fileNameToGet);
		}

		return file.toString();
	}

	public void tearDownConnection() {
		try
		{
			socketWriter.close();
			socketReader.close();
		}
		catch (IOException e)
		{
			System.out.println("Error tearing down socket: " + e);
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值