Java Socket
博客专区 > C-Kellen 的博客 > 博客详情
Java Socket
C-Kellen 发表于3个月前
Java Socket
  • 发表于 3个月前
  • 阅读 12
  • 收藏 0
  • 点赞 0
  • 评论 0

流程介绍

SocketServer

package com.socket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SocketServer {
	public static String _pattern = "yyyy-MM-dd HH:mm:ss SSS";
	public static SimpleDateFormat format = new SimpleDateFormat(_pattern);
	// 设置超时间
	public static int _sec = 0;

	public static void main(String[] args) {
		System.out.println("----------SocketServer----------");
		System.out.println(format.format(new Date()));

		ServerSocket server;
		try {
			server = new ServerSocket(8001);
			System.out.println("监听建立 等你上线\n");

			Socket socket = server.accept();
			System.out.println(format.format(new Date()));
			System.out.println("建立了链接\n");

			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

			socket.setSoTimeout(_sec * 1000);
			System.out.println(format.format(new Date()) + "\n" + _sec + "秒的时间 快写\n");

			System.out.println(format.format(new Date()) + "\nSocketClient:" + br.readLine() + "\n");

			Writer writer = new OutputStreamWriter(socket.getOutputStream());
			
			System.out.println(format.format(new Date()));
			System.out.println("我在写回复\n");
			
			writer.write("收到\n");

			Thread.sleep(10000);
			writer.flush();
			
			System.out.println(format.format(new Date()));
			System.out.println("写完啦 你收下\n\n\n\n\n");
		} catch (SocketTimeoutException e) {
			System.out.println(format.format(new Date()) + "\n" + _sec + "秒没给我数据 我下啦\n\n\n\n\n");
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

SocketClient

package com.socket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SocketClient {
	public static String _pattern = "yyyy-MM-dd HH:mm:ss SSS";
	public static SimpleDateFormat format = new SimpleDateFormat(_pattern);
	// 设置超时间
	public static int _sec = 5;

	public static void main(String[] args) {
		System.out.println("----------SocketClient----------");

		Socket socket = null;
		try {
			// 与服务端建立连接
			socket = new Socket("127.0.0.1", 8001);
			socket.setSoTimeout(_sec * 1000);

			System.out.println(format.format(new Date()));
			System.out.println("建立了链接\n");

			// 往服务写数据
			Writer writer = new OutputStreamWriter(socket.getOutputStream());
						
			System.out.println(format.format(new Date()));
			System.out.println("我在写啦\n");
			Thread.sleep(10000);
			writer.write("有没有收到\n");
			
			System.out.println(format.format(new Date()));
			System.out.println("写完啦 你收下\n");
			
			writer.flush();
			
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

			
			System.out.println(format.format(new Date()) + "\n" + _sec + "秒的时间 告诉我你收到了吗\n");

			System.out.println(format.format(new Date()) + "\nSocketServer:" + br.readLine());
			
		} catch (SocketTimeoutException e) {
			System.out.println(format.format(new Date()) + "\n" + _sec + "秒没收到回复 我下啦\n\n\n\n\n");
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

共有 人打赏支持
粉丝 17
博文 63
码字总数 80242
×
C-Kellen
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: