java Socket

来源:互联网 发布:快手软件手机版 编辑:程序博客网 时间:2024/05/22 06:08

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);//3ds.send(dp);//4while(!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("接收失败");}}}


0 0
原创粉丝点击