问题描述
网络课设作业,利用java来实现UDP,做了个小程序,第一次运行完全正确,等到第二次运行,就出现了问题,每次点“发送”按钮,都会停止在那里,没有错误提示,不知道是运算速度慢,结果还没出来,还是肿么一个情况,请大神给予解答哈……因每个帖子最多长度为10000个字符,所以分两个帖子发。呜呜呜……服务器端:UDPServerBean.javapackageUDPServer;importjava.io.*;importjava.net.*;publicclassUDPServerBean{privateDatagramSocketdSocket;privateintClientPort;privateintServerPort;privateInetAddressServerIP;privateInetAddressClientIP;privateStringcontent;//无参构造函数publicUDPServerBean()throwsSocketException,UnknownHostException{ClientPort=1111;ServerPort=1001;content="";ClientIP=InetAddress.getLocalHost();ServerIP=InetAddress.getLocalHost();dSocket=newDatagramSocket(ServerPort);}//信息发送函数,将接收到的信息发回给用户publicvoidsendToClient()throwsIOException{byte[]Buffer=("服务器已经收到:n"+content).getBytes();//将要发送的信息给Buffer变量DatagramPacketdPacket=newDatagramPacket(Buffer,Buffer.length,getClientIP(),getClientPort());//创建DatagramPacket对象dPacket,并设置客户机的IP地址与端口号dSocket.send(dPacket);//发送信息}//以下全是UDPServerBean类的各个成员变量的get和set方法publicInetAddressgetServerIP(){returnServerIP;}publicvoidsetServerIP(InetAddressserverIP)throwsException{ServerIP=serverIP;}publicDatagramSocketgetdSocket(){returndSocket;}publicvoidsetdSocket(DatagramSocketdSocket){this.dSocket=dSocket;}publicintgetClientPort(){returnClientPort;}publicvoidsetClientPort(intclientPort){ClientPort=clientPort;}publicintgetServerPort(){returnServerPort;}publicvoidsetServerPort(intserverPort)throwsSocketException{ServerPort=serverPort;}publicStringgetContent(){returncontent;}publicvoidsetContent(Stringcontent){this.content=content;}publicInetAddressgetClientIP(){returnClientIP;}publicvoidsetClientrIP(InetAddressclientIP){ClientIP=clientIP;}}
UDPServer.javapackageUDPServer;importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;importjavax.swing.*;publicclassUDPServerextendsJApplet{privateUDPServerBeanserver;privateThreadthread;privateJTextFieldjtf_ServerPort=newJTextField(10);privateJButtonjbt_Strat=newJButton("启动");privateJButtonjbt_Exit=newJButton("退出");privateJTextAreajta_Server=newJTextArea();publicUDPServer(){JPaneljplServer11=newJPanel();jplServer11.add(newJLabel("服务器端口:"));jplServer11.add(jtf_ServerPort);JPaneljplServer21=newJPanel();jplServer21.add(jbt_Strat);jplServer21.add(jbt_Exit);JPaneljplServer0=newJPanel();jplServer0.setLayout(newGridLayout(2,1));jplServer0.add(jplServer11,BorderLayout.NORTH);jplServer0.add(jplServer21);add(jplServer0,BorderLayout.NORTH);add(newJScrollPane(jta_Server),BorderLayout.CENTER);//使用线程thread=newThread(newRunnable(){publicvoidrun(){receiveForemClient();//调用发送函数}});//启动按钮事件jbt_Strat.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){intserPort=Integer.parseInt(jtf_ServerPort.getText());//从jtf_ServerPort文本区中取服务器的端口号try{server=newUDPServerBean();//创建服务器UDPServerBean的类对象server.setServerPort(serPort);//将取得的服务器端口serPort给server对象jta_Server.setText("设置服务器端口为"+jtf_ServerPort.getText()+",服务器开启...n");//将服务器端设置好的信息显示在jta_Server文本域中thread.start();//启动线程}catch(SocketExceptione2){e2.printStackTrace();}catch(UnknownHostExceptione1){e1.printStackTrace();}catch(Exceptione1){e1.printStackTrace();}}});//退出按钮的触发事件jbt_Exit.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){System.exit(0);}});}//接收客户端的信息,并将接收到的信息发回给客户机publicvoidreceiveForemClient(){Stringrec_str=null;byte[]Buffer=newbyte[1024];try{server.setdSocket(newDatagramSocket(server.getServerPort(),server.getServerIP()));//server对象调用setdSocket()函数,创建新Socket对象(此时服务器端口号为设定的端口号)}catch(SocketExceptione){e.printStackTrace();}DatagramPacketdPacket=newDatagramPacket(Buffer,Buffer.length);//创建DatagramPacket对象dPacketwhile(true){//用循环监听信息接收try{server.getdSocket().receive(dPacket);//接受信息,将接收到的信息存放在dPacket对象中rec_str=newString(dPacket.getData(),0,dPacket.getLength());//取出dPacket对象中接收到的信息server.setClientPort(dPacket.getPort());//将dPacket对象中包含的客户机的端口号给server对象server.setClientrIP(dPacket.getAddress());//将dPacket对象中包含的客户机的IP给server对象server.setContent(rec_str);//将接收的信息给server对象jta_Server.setText(jta_Server.getText()+"收到IP地址为"+server.getClientIP()+",端口为"+server.getClientPort()+"的客户机的信息有:n"+rec_str+"n");//将客户机的信息与接收的信息显示在jta_Server文本域中server.sendToClient();//将信息发送回去}catch(IOExceptione){e.printStackTrace();}}}}
解决方案
解决方案二:
要有耐心,仔细检查
解决方案三:
是一层10000字谁告诉你一帖10000字。。你不会分两楼发啊。。
解决方案四:
引用2楼vnvlyp的回复:
是一层10000字谁告诉你一帖10000字。。你不会分两楼发啊。。
受教,受教,下次注意……
解决方案五:
昨天才写的TCPUDP的通信你可以看看新手勿喷