服务器端发送UDP数据包客户端未收到问题解决

最近跟着马士兵老师学习坦克大战网络版的编写,在昨晚的编写过程中遇到了问题,代码如下,服务器端接收客户端发来的坦克的信息并转发给所有的坦克时,客户端没有收到UDP消息,

1,考虑是客户端的接收udp包的线程没有启动么?于是在线程类UDPRecvThread的run方法中System.out.println("something"),运行程序,有输出,说明线程已经启动;

2,初次尝试使用一下debug,正常情况下程序应该输出“a packet received from server”,现在没有输出,考虑将断点设置在输出语句的上一句ds.receive(dp),执行debug,程序执行到receive后再Step over,程序直接结束了,考虑服务器端是否将udp包发出来了

3,服务器端是否将udp包发送出来,在ds.send(dp)后输出一句“a packet sended”,并解析该数据包,执行程序发现有输出语句,解析的包也没有错误,说明客户端的坦克信息包发送成功且数据包没有问题,服务器端也接收成功,出错环节就在服务器往各个客户端发送,

4,查看ArrayList中是否加入了客户端坦克,检查之后也是没有问题

5,没有办法与马老师的源代码逐行对比,对比代码也是没有发现问题

以上就是昨晚的大致思路了,已经1点了,遂洗洗碎了;

闭上眼睛睡不着,整理一下思路,再考虑考虑,问题就出在服务端发往客户端的过程中,客户端没收到,服务端的发送地址是否写对了,这个以上没有考虑,服务端在发送udp包之前有对udp包重新设置IP和端口,取的是坦克的IP和端口,应该没错,那坦克将自身的IP和端口传送给服务端时有没有出错呢?明天检查吧

今晚回来第一时间查看端口设置,服务器端中start方法中readInt进来的udpPort也没有错;

在往下看问题出在保存坦克IP和端口的类中,

Client c = new Client(IP,udpPort);//new一个类对象

/**
如下是类定义,类的构造方法中由于粗心,udp写成upd
在eclipse中
**/
	private class Client{
		String IP;
		int udpPort;
		public Client(String IP , int updPort){
			this.IP = IP;
			this.udpPort = udpPort;//在eclipse中该句被标了下划波浪线,是个警告,单击后提示“Creat parameter udpPort”,这才发现问题
		}
	}

 
  

//服务器端
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
public class TankServer {
	
	public static final int TCP_PORT = 8888;
	public static final int UDP_PORT = 6666;
	private static int ID = 100;
	List clients  = new ArrayList();
	
	
	public void start(){
		
		new Thread(new UDPThread()).start();
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(TCP_PORT);
		} catch (IOException e) {
			e.printStackTrace();
		}
		while(true) {
			Socket s = null;
			try{
				s = ss.accept();
				DataInputStream dis = new DataInputStream(s.getInputStream());
				int udpPort = dis.readInt();
				String IP = s.getInetAddress().getHostAddress();
				Client c = new Client(IP,udpPort);
				clients.add(c);
				DataOutputStream dos = new DataOutputStream(s.getOutputStream());
				dos.writeInt(ID++);
System.out.println("a Client is connected:"+s.getInetAddress()+":"+ s.getPort()+"----- udp Port:"+udpPort);
			}catch(IOException e){
					e.printStackTrace();
			}finally{
				if(s!=null){
					try {
						s.close();
						s = null;
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
			}
				
		}
	}
	
	public static void main(String[] args) {
		
		new TankServer().start();
		
	}

	private class Client{
		String IP;
		int udpPort;
		public Client(String IP , int udpPort){
			this.IP = IP;
			this.udpPort = udpPort;
		}
	}
	
	private class UDPThread implements Runnable {

		byte[] buf = new byte[1024];
		
		@Override
		public void run() {
			DatagramSocket ds = null;
			try {
				ds = new DatagramSocket(UDP_PORT);
			} catch (SocketException e) {
				e.printStackTrace();
			}
System.out.println("UDP thread started at port :"+ UDP_PORT);
			while(ds!=null){
				DatagramPacket dp = new DatagramPacket(buf,buf.length);
				try {
					ds.receive(dp);
					for(int i=0;i

//客户端网络连接类,NetClient
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;


public class NetClient {
	
	private static int UDP_PORT_START = 2223;
	private int udpPort;
	TankClient tc;
	DatagramSocket ds = null;
	
	public NetClient(TankClient tc){
		 udpPort = UDP_PORT_START ++;
		 this.tc = tc;
		 try {
			ds = new DatagramSocket(udpPort);
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}
	
	public void connect(String IP, int port) {
		Socket s = null;
		try {
			s = new Socket(IP,port);
			DataOutputStream dos = new DataOutputStream(s.getOutputStream());
			dos.writeInt(udpPort);
			DataInputStream dis = new DataInputStream(s.getInputStream());
			int id = dis.readInt();
			tc.t.id = id;
System.out.println("connect to server ! ID:"+ id);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(s!=null){
				try {
					s.close();
					s = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		TankNewMsg msg = new TankNewMsg(tc.t);
		send(msg);
		new Thread(new UDPRecvThread()).start();
	}
	
	public void send(TankNewMsg msg){
		msg.send(ds,"localhost",TankServer.UDP_PORT);
	}
	
	private class UDPRecvThread implements Runnable{

		byte[] buf = new byte[1024];
		@Override
		public void run() {
			while(ds!=null){
				DatagramPacket dp = new DatagramPacket(buf,buf.length);
				try {
					ds.receive(dp);
					parse(dp);
System.out.println("a packet received from server");

				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		private void parse(DatagramPacket dp) {
			ByteArrayInputStream bais = new ByteArrayInputStream(buf,0,dp.getLength());
			DataInputStream dis = new DataInputStream(bais);
			TankNewMsg msg = new TankNewMsg();
			msg.parse(dis);
			
		}
		
	}
}


你可能感兴趣的:(j2se,java,服务器,udp,网络)