码迷,mamicode.com
首页 > 编程语言 > 详细

Java之------socket系列(三)

时间:2016-05-12 23:52:28      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

练习:UDP聊天程序

通过键盘录入获取要发送的信息。
将发送和接收分别封装到两个线程中。


聊天方一:

<span style="font-size:14px;">package cn.hncu.url.udp;

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

import com.sun.imageio.plugins.common.InputStreamAdapter;


public class UDPChat {

	public static void main(String[] args) {
		try {
			DatagramSocket send=new DatagramSocket(10001);
			DatagramSocket receive=new DatagramSocket(10002);
			
			new Thread(new Send(send)).start();
			new Thread(new Receive(receive)).start();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		
	}
}

class Send implements Runnable{
	private DatagramSocket ds=null;
	public Send(DatagramSocket ds){
		this.ds=ds;
	}
	
	@Override
	public void run() {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String line=null;
		try {
			while ((line=br.readLine())!=null){
				byte buf[]=line.getBytes();
				//下面的ip地址我用xxx.xxx.xxx.xxx表示,这里要使用你自己的ip地址,运行时记得改过来
				DatagramPacket dp=new DatagramPacket(buf, buf.length,InetAddress.getByName("</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px;">xxx.xxx.xxx.xxx</span></span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px;">"),10004);</span></span><span style="font-size:14px;">
				ds.send(dp);
				if (line.equalsIgnoreCase("over")){
					break;
				}
			}
//			ds.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}

class Receive implements Runnable{
	private DatagramSocket ds=null;
	public Receive(DatagramSocket ds){
		this.ds=ds;
	}
	
	@Override
	public void run() {
		byte buf[]=new byte[1024];
		try {
			while (true){
				DatagramPacket dp=new DatagramPacket(buf, buf.length);
				ds.receive(dp);
				byte data[]=dp.getData();
				String ip=dp.getAddress().getHostAddress();
				String info=new String(data);
				System.out.println(ip+" says "+info);
				if (info.equalsIgnoreCase("over")){
					System.out.println(ip+" has left the chatting room......");
				}
			}
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}</span>
聊天方二:

package cn.hncu.url.udp.chat2;

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

import com.sun.imageio.plugins.common.InputStreamAdapter;


public class UDPChat2 {

	public static void main(String[] args) {
		try {
			DatagramSocket send=new DatagramSocket(10003);
			DatagramSocket receive=new DatagramSocket(10004);
			
			new Thread(new Send(send)).start();
			new Thread(new Receive(receive)).start();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		
	}
}

class Send implements Runnable{
	private DatagramSocket ds=null;
	public Send(DatagramSocket ds){
		this.ds=ds;
	}
	
	@Override
	public void run() {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String line=null;
		try {
			while ((line=br.readLine())!=null){
				byte buf[]=line.getBytes();
				//下面的ip地址我用xxx.xxx.xxx.xxx表示,这里要使用你自己的ip地址,运行时记得改过来
				DatagramPacket dp=new DatagramPacket(buf, buf.length,InetAddress.getByName("<span style="font-family: Arial, Helvetica, sans-serif;">xxx.xxx.xxx.xxx</span><span style="font-family: Arial, Helvetica, sans-serif;">"),10002);</span>
				ds.send(dp);
				if (line.equalsIgnoreCase("over")){
					break;
				}
			}
			ds.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}

class Receive implements Runnable{
	private DatagramSocket ds=null;
	public Receive(DatagramSocket ds){
		this.ds=ds;
	}
	
	@Override
	public void run() {
		byte buf[]=new byte[1024];
		try {
			while (true){
				DatagramPacket dp=new DatagramPacket(buf, buf.length);
				ds.receive(dp);
				byte data[]=dp.getData();
				String ip=dp.getAddress().getHostAddress();
				String info=new String(data);
				System.out.println(ip+" says "+info);
				if (info.equalsIgnoreCase("over")){
					System.out.println(ip+" has left the chatting room......");
				}
			}
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}


Java之------socket系列(三)

标签:

原文地址:http://blog.csdn.net/x121850182/article/details/51346258

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!