老师跪求多线程UDP代码QuQ,表示写的代码总是有错

来源:4-3 编程实现基于 UDP 的 Socket 通信之客户端

鲁鲁修STANDBYU

2015-03-20 22:04

package pers.Sockets;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;

// UDP服务器线程处理累
 
public class UDPServerThread extends Thread {
		DatagramPacket packet=null;
		DatagramSocket socket=null;
		byte[] data=null;
		public UDPServerThread(DatagramPacket packet,DatagramSocket socket,byte[] data){
			this.packet=packet;
			this.socket=socket;
			this.data=data;
		}
		//线程执行操作,响应客户端的请求
		public void run(){
			try {
				DatagramPacket  packet2=new DatagramPacket("欢迎您!".getBytes(), "欢迎您!".getBytes().length, packet.getAddress(),packet.getPort());;
				//4.读取数据
				String info=new String(data,0,packet.getLength());
				System.out.println("我是服务器,客户端说:"+info);
///****          向客户端做出响应没有实现,仍存在问题         ****/
				/*
				 * 向客户端做出响应
				 */
				//2.创建数据报、包含响应的数据信息
				/*    packet2    */
				//3.响应客户端
				System.out.println("已响应客户端!"+UDPServerT.count);	
				socket.send(packet2);

//			//4.关闭资源
//			socket.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
}


写回答 关注

3回答

  • 隔壁王爷爷
    2015-04-08 18:09:39
    package com.imooc.socket.UDP;
    //这个是Server 服务器main的地方
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    
    /**
     * Created with IntelliJ IDEA.
     * User: QiuShiLe
     * Date: 2015/4/8
     * Time: 17:11
     * Project: coreJava
     * Detail:实现基于UDP的用户登录
     */
    public class Server {
        public static void main(String[] args) throws IOException {
            DatagramSocket socket = new DatagramSocket(8888);
            byte[] data = new byte[1024];
            DatagramPacket packet;
            packet = new DatagramPacket(data, data.length);
            System.out.println("服务器已启动");
            while (true){
                socket.receive(packet);//在接收到数据包之前一直阻塞
                ServerThread thread = new ServerThread(socket, packet);
                thread.run();
            }
        }
    }
    package com.imooc.socket.UDP;
    //这是服务器处理请求的子线程
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    /**
     * Created with IntelliJ IDEA.
     * User: QiuShiLe
     * Date: 2015/4/8
     * Time: 17:35
     * Project: coreJava
     * Detail:Server的线程实现
     */
    public class ServerThread implements Runnable {
    
        private DatagramSocket socket;
        private DatagramPacket packet;
    
        public ServerThread(DatagramSocket socket, DatagramPacket packet) {
            this.socket = socket;
            this.packet = packet;
        }
    
        @Override
        public void run() {
    
            byte[] data = packet.getData();
    
            String info = new String (data, 0, packet.getLength());
            System.out.println("我是服务器,客户端说:" + info);
    
            InetAddress address = packet.getAddress();
            int port = packet.getPort();
            byte[] data2 = "欢迎您!".getBytes();
            DatagramPacket reply = new DatagramPacket(data2, data2.length, address, port);
            try {
                socket.send(reply);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


  • 想你的俊
    2015-03-27 22:57:49

    package demostrator;


    import java.io.IOException;

    import java.net.DatagramPacket;

    import java.net.DatagramSocket;

    import java.net.InetSocketAddress;


    public class UDPserver {

    public static void main(String[] args) throws IOException {

    InetSocketAddress sd = new InetSocketAddress("localhost", 8888);

    DatagramSocket socket = new DatagramSocket(sd);

    System.out.println("***Server will be start!***");

    while (true) {

    byte[] data = new byte[1024];

    DatagramPacket packet = new DatagramPacket(data, data.length);

    socket.receive(packet);

    ServerThread thread = new ServerThread(packet,socket);

    new Thread(thread).start();

    }

    }


    }


    package demostrator;


    import java.io.IOException;

    import java.net.DatagramPacket;

    import java.net.DatagramSocket;

    import java.net.InetAddress;



    class ServerThread implements Runnable{

    private DatagramPacket packet = null;

    private DatagramSocket socket = null;

    ServerThread(DatagramPacket packet,DatagramSocket socket){

    this.packet = packet;

    this.socket = socket;

    }


    @Override

    public void run() {

    String info = new String(packet.getData());

    //output info from client

    System.out.println("i am server,client say: "+info);

    InetAddress clientAddress = packet.getAddress();

    int port = packet.getPort();

    byte[] data1= "Welcome to server!~".getBytes();

    DatagramPacket packet1 = new DatagramPacket(data1, data1.length, clientAddress, port);

    try {

    socket.send(packet1);

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }


    /*public class ServerThread extends Thread {

    private DatagramPacket packet = null;

    private DatagramSocket socket = null;

    ServerThread(DatagramPacket packet,DatagramSocket socket){

    this.packet = packet;

    this.socket = socket;

    }

    public void run(){

    String info = new String(packet.getData());

    //output info from client

    System.out.println("i am server,client say: "+info);

    InetAddress clientAddress = packet.getAddress();

    int port = packet.getPort();

    byte[] data1= "Welcome to server!~".getBytes();

    DatagramPacket packet1 = new DatagramPacket(data1, data1.length, clientAddress, port);

    try {

    socket.send(packet1);

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    socket.close();

    }

    }*/


  • 鲁鲁修STANDBYU
    2015-03-20 22:06:12

    为什么第一次客户端运行得不到服务器回复,只有之后的能得到回复,但是得到的回复貌似也有问题。。

    想你的俊

    试试我的,我也想了好久,参考了其它人的代码才知道问题出在哪里的。

    2015-03-27 22:57:35

    共 1 条回复 >

Java Socket应用---通信是这样练成的

分享的是 Java 中的网络编程,使用Socket实现网络聊天通信

125012 学习 · 590 问题

查看课程

相似问题