黑马程序员:基于TCP协议的网络服务:ServerSocket、Socket

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

TCP传输:

Socket和ServerSocket
建立客户端和服务端
建立连接后,通过Socket中的IO流进行数据的传输
关闭socket
客户端和服务端是两个独立的应用程序

客户端:
通过查阅socket对象,发现在该对象建立时,就可以去连接指定主机
因为tcp是面向连接的,所以在建立socket服务时,
就要有服务端存在,并连接成功,形成通路后,
在该通道进行数据的传输

步骤:
1.创建Socket服务,并指定服务器主机和端口
Socket s = new Socket("192.168.2.100",10000)
或者:Socket s = new Socket(InetAddress.getByName("192.168.2.100"),10000);
2.通过Socket对象获取socket流中的输出流(要往服务端写数据)
OutputStream out = s.getOutputStream();
out.write("send to server".getBytes());
3.获取socket流中的输入流,将服务端反馈的数据获取到,并打印
4.关闭资源
s.close();
代码演示:
class TcpClient
{
	public static void main(String[] args) throws Exception
	{
		Socket s = new Socket("192.168.2.100",10000);
		OutputStream os = s.getOutputStream();
		os.write("send to server".getBytes());
		InputStream is = s.getInputStream();
		byte[] buf = new byte[1024];
		int len = 0;
		len=is.read(buf); //这里如果while循环,read会进入阻塞状态,一直等待服务端的信息
		System.out.println(new String(buf,0,len));
		s.close();
	}
}

需求:定义端点接收数据并打印在控制台上
服务端:
ServerSocket其中两个常用构造方法
ServerSocket(int port)
ServerSocket(int port, int backlog) backlog是指该服务支持多少客户端同时在线数,即并发数,一般不超过50,看机器性能
1.建立服务端的socket服务,ServerSocket(int port),并监听端口
2.获取连接过来的客户端对象,通过ServerSocket的accept方法完成,该方法是阻塞式方法
3.服务端若接收到客户端数据,则需要通过客户端对象的读取流读取发送过来的数据并打印在控制台
4.通过客户端对象的输出流给客户端反馈信息
5.关闭资源(自己可选,但要把调用的客户端对象关了) 
代码演示:
class TcpServer
{
	public static void main(String[] args) throws Exception
	{
		ServerSocket ss = new ServerSocket(10000);
		Socket s = ss.accept();
		InputStream is = s.getInputStream();
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip);
		byte[] buf =  new byte[1024];
		int len = 0;
		len=is.read(buf);//这里如果while循环,read会进入阻塞状态,一直等待客户端的信息,或者while循环用available()判断
		System.out.println(new String(buf,0,len));
		OutputStream os = s.getOutputStream();
		Thread.sleep(5000);//这里睡5秒,客户端的read会进入阻塞状态,等待服务端的反馈
		os.write("send to client".getBytes());
		s.close(); //关闭客户端对象
		ss.close(); //关闭服务器 这里可选,可关可不关
	}
}

注:UDP中开启顺序任意,但TCP是面向连接,所以 先要开启服务器端,否则客户端连接不上,会出现java.net.ConnectException:拒绝连接

需要注意的现象:
客户端和服务端都在等待的原因?
客户端和服务端都有阻塞式的方法,这些方法若没读到结束标记,那么就一直等,从而导致两端都在等待
注意缓冲区需要flush()到流中,ReadLine()需要读取到换行标记才算读完一行,所以输出流中写入newLine();而结束标记有两种方法:
1.Socket提供的,也是最常用的  shutdownInput(); shutdownOutput();
2.自定义一个标记符号,在传输数据之前,先把结束标记符给服务器,在传输完数据之后,客户端把原先设计好的结束标记符发送给服务端,服务端在循环中判断接收到了和结束标记相同的符号时,结束循环
注意:结束标记可以用字符串over或者其他表示,但是往往这种标记会和发送的内容出现相同,从而导致数据未传输完毕就结束了。所以这时候就可以通过传输一个时间戳给服务端 比如 long time = System.currentTimeMillis(); DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeLong(time); 


若非交互式,使用while判断是否到达流末尾时,可以通过available()来判断,它要==0,则已到达流末尾(InputStream及子类支持该方法)


使用TCP协议写一个可以上传文件的服务器和客户端:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

/**
 * 使用TCP协议写一个可以上传文件的服务器和客户端。
 * 
 * @author rudy
 * 
 */
public class UpLoadFile {

	public static void main(String[] args) throws Exception {
	}

}

class FileServer {
	public static void main(String[] args) throws Exception {
		@SuppressWarnings("resource")
		ServerSocket fileServer = new ServerSocket(20005);
		while (true) {
			Socket client = fileServer.accept();
			new Thread(new FileServerThread(client)).start();
		}
	}
}

// 服务器线程:供多用户连接,每个用户连接上服务器则创建一个新线程
class FileServerThread implements Runnable {
	private Socket client;

	FileServerThread(Socket client) {
		this.client = client;
	}

	public void run() {
		try {
			System.out.println(client.getInetAddress().getHostAddress()
					+ " 开始上传文件");
			BufferedInputStream readFile = new BufferedInputStream(
					client.getInputStream());
			byte[] buf = new byte[1024];
			// 读取扩展名
			readFile.read(buf, 0, 3);
			String expandName = new String(buf, 0, 3);
			Random ran = new Random();
			File file = new File("c:", Math.abs(ran.nextLong()) + "."
					+ expandName);
			while (file.exists()) {
				file = new File("c:", Math.abs(ran.nextLong()) + "."
						+ expandName);
			}
			BufferedOutputStream writeFile = new BufferedOutputStream(
					new FileOutputStream(file));
			int len = 0;
			while (readFile.available() != 0) {
				len = readFile.read(buf);
				writeFile.write(buf, 0, len);
				writeFile.flush();
			}
			System.out.println("上传完毕,文件地址:" + file.getPath());

			BufferedWriter result = new BufferedWriter(new OutputStreamWriter(
					new PrintStream(client.getOutputStream())));
			result.append("上传成功");
			// result.newLine();
			result.flush();
			readFile.close();
			writeFile.close();
			result.close();
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

// 客户端
class Client {
	private Socket client;
	private BufferedInputStream readFile;
	private BufferedOutputStream writeFile;
	private BufferedReader resultbuf;

	public static void main(String[] args) {
		new Client().uploadFile(args[0]);
	}

	/**
	 * 上传文件的方法
	 * 
	 * @param args
	 */
	public void uploadFile(String args) {

		/* 判断传入的是否是一个存在的、小于等于5M、扩展名为txt或者jpg的文件 */
		File file = new File(args);
		if (!(file.isFile() || file.exists())) {
			System.out.println("请输入正确的文件地址");
			return;
		}
		if (file.length() > 1024 * 1024 * 5) {
			System.out.println("超过文件上限5M");
			return;
		}
		String expandName = file.getName().substring(
				file.getName().lastIndexOf('.') + 1);
		if (!(expandName.equals("txt") || expandName.equals("jpg"))) {
			System.out.println("该程序只允许上传txt、jpg文件");
			return;
		}

		try {
			client = new Socket("192.168.2.105", 20005);
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			writeFile = new BufferedOutputStream(client.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			readFile = new BufferedInputStream(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		// 将文件扩展名写入到socket输入流
		try {
			writeFile.write(expandName.getBytes(), 0, 3);
		} catch (IOException e) {
			System.out.println("写入文件扩展名失败");
		}
		try {
			writeFile.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] buf = new byte[1024];
		int len = 0;
		try {
			while ((len = readFile.read(buf)) != -1) {
				writeFile.write(buf, 0, len);
				writeFile.flush();
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		try {
			resultbuf = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		String line = "";
		// 在控制台中输出服务器返回的信息
		try {
			while ((line = resultbuf.readLine()) != null) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (resultbuf != null)
				resultbuf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (readFile != null)
				readFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (client != null)
				client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}


---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值