import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.io.*;
import java.net.*;
public class DataSocketSer
{
public static void main(String[] args)
{
String str="";
Frame f=new Frame();
f.setSize(355, 355);
f.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
Label l=new Label(" 111111111111 ");
f.add(l);
f.setVisible(true);
DatagramSocket da=null;
try
{
da=new DatagramSocket(4700);
}
catch(SocketException e)
{
e.getMessage();
}
byte b[]="wwwwwwwww".getBytes();
InetAddress address=null;
try
{
address=InetAddress.getByName("127.0.0.1");
}
catch(UnknownHostException e)
{
e.getMessage();
}
DatagramPacket dp =new DatagramPacket(b,6,address,4700);
try
{
da.send(dp);
}
catch( IOException e)
{
e.getMessage();
}
}
}import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.io.*;
import java.net.*;
public class DataSocketSer
{
public static void main(String[] args)
{
String str="";
Frame f=new Frame();
f.setSize(355, 355);
f.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
Label l=new Label(" 111111111111 ");
f.add(l);
f.setVisible(true);
DatagramSocket da=null;
try
{
da=new DatagramSocket(4700);
}
catch(SocketException e)
{
e.getMessage();
}
byte b[]="wwwwwwwww".getBytes();
InetAddress address=null;
try
{
address=InetAddress.getByName("127.0.0.1");
}
catch(UnknownHostException e)
{
e.getMessage();
}
DatagramPacket dp =new DatagramPacket(b,6,address,4700);
try
{
da.send(dp);
}
catch( IOException e)
{
e.getMessage();
}
}
}我先打开第一个程序,再打第二个程序,但是没什么反应,这是怎么一回事
import java.awt.Frame;
import java.awt.Label;
import java.io.*;
import java.net.*;
public class DataSocketSer
{
public static void main(String[] args)
{
String str="";
Frame f=new Frame();
f.setSize(355, 355);
f.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
Label l=new Label(" 111111111111 ");
f.add(l);
f.setVisible(true);
DatagramSocket da=null;
try
{
da=new DatagramSocket(4700);
}
catch(SocketException e)
{
e.getMessage();
}
byte b[]="wwwwwwwww".getBytes();
InetAddress address=null;
try
{
address=InetAddress.getByName("127.0.0.1");
}
catch(UnknownHostException e)
{
e.getMessage();
}
DatagramPacket dp =new DatagramPacket(b,6,address,4700);
try
{
da.send(dp);
}
catch( IOException e)
{
e.getMessage();
}
}
}import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.io.*;
import java.net.*;
public class DataSocketSer
{
public static void main(String[] args)
{
String str="";
Frame f=new Frame();
f.setSize(355, 355);
f.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
Label l=new Label(" 111111111111 ");
f.add(l);
f.setVisible(true);
DatagramSocket da=null;
try
{
da=new DatagramSocket(4700);
}
catch(SocketException e)
{
e.getMessage();
}
byte b[]="wwwwwwwww".getBytes();
InetAddress address=null;
try
{
address=InetAddress.getByName("127.0.0.1");
}
catch(UnknownHostException e)
{
e.getMessage();
}
DatagramPacket dp =new DatagramPacket(b,6,address,4700);
try
{
da.send(dp);
}
catch( IOException e)
{
e.getMessage();
}
}
}我先打开第一个程序,再打第二个程序,但是没什么反应,这是怎么一回事
解决方案 »
- Eclipse导出jar文件异常
- NIO得性能问题,总是出现延时~~~~
- 关于路径的问题,应该很简单,急
- 判断输入的日期是否合法,及润月问题(个人编写。仅供参考。如果有更好的思路敬请指教)
- 终于能跑通了 用java写的web服务端 处理移动的provision
- 为何会出现这个错误?
- 大家帮我看看这段代码,为什么编译会出错
- jsp中 <a>标签如何发送一个java对象(非String)给下一个页面
- ▲▲▲ 高分: 姓名 插入到表里补了几个空格,在oracle下 ▲▲▲
- 哪里可以下载到JDEVLEPOR开发工具?/
- 请教一下如何用JAVA繁简转换?急!!!
- 谁能提供一下 testle.jar, 和 stylebook.jar 我找了好久没有找到, 谢谢!
import java.net.*;
import java.awt.*;
import java.io.*;
public class DataSocket
{ public static void main(String[] args)
{
String str="";
Frame f=new Frame();
f.setSize(355, 355);
f.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
Label l=new Label(" 111111111111 ");
f.add(l);
f.setVisible(true);
DatagramSocket da=null;
try
{
da=new DatagramSocket(4700);
}
catch(SocketException e)
{
e.getMessage();
}
byte b[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(b,6);
try
{
da.receive(dp);
}
catch(IOException e)
{
e.getMessage();
}
l.setText(new String(dp.getData()));
}}
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class server extends JFrame implements Runnable,ActionListener{JTextField out;
JTextArea text;
JPanel p1;
JButton btn;
JScrollPane textPanel;
Thread tr;
InetAddress ip = null;// 构造方法:
public server(){
Container c = getContentPane();
out = new JTextField(15);
text = new JTextArea(30,30);
btn = new JButton("确定");
textPanel = new JScrollPane(text);
text.setEditable(false);
text.setLineWrap(true);
p1 = new JPanel();
p1.add(out);
p1.add(btn);
out.addActionListener(this);
btn.addActionListener(this);
c.add(textPanel,"Center");
c.add(p1,"South");
setSize(400,400);
setVisible(true);
tr = new Thread(this);
tr.start();
addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){
System.exit(0);}
});
}
public void actionPerformed(ActionEvent ae){
if((ae.getSource() == btn) || (ae.getSource() == out)){ byte buffer[] = (out.getText()).trim().getBytes();
try{
ip = InetAddress.getByName("127.0.0.1");
DatagramPacket da = new DatagramPacket(buffer,buffer.length,ip,2006);
DatagramSocket mail = new DatagramSocket();
text.append("输入:" + out.getText() + '\n');
mail.send(da);
}catch(Exception ie){}
out.setText(null);
}
} // 接收数据报线程体:
public void run(){ DatagramPacket pack = null;
DatagramSocket mail = null;
byte data[] = new byte[8192];
try{
pack = new DatagramPacket(data,data.length);
mail = new DatagramSocket(2006);
}catch(Exception ie){} while(true){ if(mail == null)
break;
else
try{
mail.receive(pack);
int len = pack.getLength();
ip = pack.getAddress();
String datas = new String(pack.getData(),0,len);
Thread.sleep(3000);
this.text.append("接收:" + datas +'\n');
}catch(Exception ie){}
} } public static void main(String args[]){
server ser = new server();
} }
看你的都晕了,这是我前年练习的时间写的自发自收的小程序,接收是用线程实现的,参考下,