亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Socket編程

標簽:
Java

Socket概述
Socket概述
UDP
UDP
UDP数据传输流程:
UDP数据传输流程
以下是代码实现:
发送方UDP
接收方UDP
因为UDP是无连接的不可靠传输,所以接收方需要在发送方发送数据之前就启动,否则会接收不到数据。也就是说必须先运行UDPSocketReceive再运行UDPSocketSend。控制台打印如下:
发送方:

Sender Start...

接受方:

Receiver Start...
address:/127.0.0.1---port:61177---content:God bless you!

聊天实现:

package Socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class SocketFemale {
	public static void main(String[] args) {
		try {
			new Thread(new Runnable() {
				@Override
				public void run(){
					try {
						receive();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}).start();
			send();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//接收消息方法
	private static void receive() throws IOException {
		System.out.println("SocketFemale Receiver Start...");

		//1.创建udp的socket服务,并声明端口号
		DatagramSocket dsReceive = new DatagramSocket(6666);
		//无限循环,一直处于接收状态
		while (true) {
			//2.创建接收数据的数据包
			byte bytes[] = new byte[1024];
			DatagramPacket dp = new DatagramPacket(bytes, bytes.length);

			//3.将数据接收到数据包中
			dsReceive.receive(dp);

			//4.解析数据
			String content = new String(dp.getData(), 0, dp.getLength());
			System.out.println("SocketFemale Receive:" + content);
		}
	}

	private static void send() throws IOException {
		//1.创建socket服务
		DatagramSocket dsSend = new DatagramSocket();

		//将键盘输入的信息转换成输入流再放入到缓冲区
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = null;
		while ((line=br.readLine())!=null){
			//2.封装数据
			byte[] bytes = line.getBytes();
			//地址
			InetAddress address =InetAddress.getByName("127.0.0.1");
			//参数:数据、长度、地址、端口
			DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,7777);

			//3.发送数据包
			dsSend.send(dp);
		}
		//4.关闭socket服务
		dsSend.close();
	}
}
package Socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class SocketMale {
	public static void main(String[] args){
		try {
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						receive();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}).start();
			send();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//接收消息方法
	private static void receive() throws IOException {
		System.out.println("SocketMale Receiver Start...");

		//1.创建udp的socket服务,并声明端口号
		DatagramSocket dsReceive = new DatagramSocket(7777);
		//无限循环,一直处于接收状态
		while (true) {
			//2.创建接收数据的数据包
			byte[] bytes = new byte[1024];
			DatagramPacket dp = new DatagramPacket(bytes, bytes.length);

			//3.将数据接收到数据包中
			dsReceive.receive(dp);

			//4.解析数据
			String content = new String(dp.getData(), 0, dp.getLength());
			System.out.println("SocketMale Receive:" + content);
		}
		//关闭服务
		// ds.close;
	}

	private static void send() throws IOException {
		//1.创建socket服务
		DatagramSocket dsSend = new DatagramSocket();

		//将键盘输入的信息转换成输入流再放入到缓冲区
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = null;
		while ((line=br.readLine())!=null){
			//2.封装数据
			byte[] bytes = line.getBytes();
			//地址
			InetAddress address =InetAddress.getByName("127.0.0.1");
			//参数:数据、长度、地址、端口
			DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,6666);

			//3.发送数据包
			dsSend.send(dp);
		}
		//4.关闭socket服务
		dsSend.close();
	}
}

TCP
TCP
TCP聊天实现:
一个服务端可以同时和多个客户端进行通信。如何区分不同客户端?服务端首先通过accept获取到客户端Socket,然后通过客户端的Socket获取的流进行通讯,服务端得以区分每个客户端。TCP服务端
TCP客户端

點擊查看更多內容
1人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
JAVA開發工程師
手記
粉絲
37
獲贊與收藏
353

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消