java Socket

本文介绍了一个简单的UDP聊天程序的设计与实现过程。该程序包括发送端和接收端两部分,能够实现在局域网内的双向文本消息传递。文章详细展示了如何使用Java的DatagramSocket进行UDP数据包的发送与接收,并通过多线程技术实现了消息的同步处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Socket Send

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
/*
 * 通过udp转输方式,将一段文字数据发送出去
 * 1.建立udpsocket服务
 * 2.提供数据,并将数据封装到数据包中
 * 3.通过socket发送
 * 4.关闭资源
 * */
public class UdpSend {
	public static void main(String[] args) throws Exception{
		Scanner scanner = new Scanner(System.in);
		
		String ip = scanner.nextLine();
		String data = scanner.nextLine();
		System.out.println("Input Server port");
		int port = scanner.nextInt();
		//1.
		DatagramSocket ds =new DatagramSocket();
		//2/
		byte[] buf =data.getBytes();
		
		DatagramPacket dp =new DatagramPacket(buf, buf.length, InetAddress.getByName(ip), port);
		//3
		ds.send(dp);
		//4
		while(!data.equals("bye")){
			data = scanner.nextLine();
			buf = data.getBytes();
			dp =new DatagramPacket(buf, buf.length, InetAddress.getByName(ip),port);
			ds.send(dp);		
		}
		ds.close();
		scanner.close();
	}
}


Socket Recieve

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpRecieve{
	public static void main(String[] args) throws Exception{
		//
		DatagramSocket ds = new DatagramSocket(10004);
		//
		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf, buf.length);
		//
		ds.receive(dp);
		//
		String ip = dp.getAddress().getHostAddress();
		
		String data = new String(dp.getData(), 0, dp.getLength());
		
		int port = dp.getPort();
		
		System.out.println("从端口:"+port+"接收到来自:"+ip+"的信息:\n"+data);
		while(! data.equals("bye")){
			ds.receive(dp);
			data = new String(dp.getData(), 0, dp.getLength());
			System.out.println("从端口:"+dp.getPort()+"接收到来自:"+dp.getAddress()+"的信息:\n"+data);
		}
		ds.close();
	}
}


Socket Send&Recieve

package 局域网聊天;

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

public class Chat {
	public static void main(String[] args) throws Exception{
		DatagramSocket sendSocket = new DatagramSocket();
		DatagramSocket receSocket = new DatagramSocket(10002);
		
		
		System.out.println("开始聊天");
		
		new Thread(new Send(sendSocket)).start();
		new Thread(new Rece(receSocket)).start();
	}
}

class Send implements Runnable{
	private DatagramSocket ds;
	public  Send(DatagramSocket ds){
		this.ds = ds;
	}
	public void run(){
		try{
			BufferedReader bufr = new BufferedReader(new  InputStreamReader(System.in));
			
			String line = null;
			
			while( (line = bufr.readLine() ) !=null){
				if( "886".equals(line) )break;
				
				byte[] buf = line.getBytes();
				
				DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.255"), 10002);
				
				ds.send(dp);
			}
		}catch(Exception e){
			throw new RuntimeException("发送失败");
		}
		ds.close();
	}
}

class Rece implements Runnable{
	private DatagramSocket ds;
	public  Rece(DatagramSocket ds){
		this.ds = ds;
	}
	public void run(){
		try{
			while(true){
				byte[] buf = new byte[1024];
				DatagramPacket dp =new DatagramPacket(buf, buf.length);
				
				ds.receive(dp);
				
				String ip = dp.getAddress().getHostAddress();
				String data = new String(dp.getData(), 0, dp.getLength());
				System.out.println(ip+":\n"+data);
			}
		}catch(Exception e){
			throw new RuntimeException("接收失败");
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值