java通信
下面是一个client程序
服务器和客户在一台机子上
客户端发送一个字符串
服务器端用 vb Winsock控件接收
这个程序还是有问题
不能发送,搞了几天了
望高手指点
import java.lang.Thread;
import java.io.*;
import java.net.*;
public class Client
{
private DatagramSocket s;
private byte[] buf=new byte[1000];
private InetAddress hostAddress;
private DatagramPacket dp=new DatagramPacket(buf,buf.length);
public static DatagramPacket toDatagram(String s,InetAddress destIA,int destPort){
byte[] buf=new byte[s.length()+1];
s.getBytes(0,s.length(),buf,0);
return new DatagramPacket(buf,buf.length,destIA,destPort);
}
public Client(){
try{
s=new DatagramSocket();
hostAddress=InetAddress.getByName("localhost");
s.send(toDatagram("China",hostAddress,10000));
}
catch(SocketException e)
{
System.out.println("can't open socket!");
System.exit(1);
}
catch(IOException e){
System.out.println("error!");
System.exit(1);
}
}
public static void main(String args[]){
Client c=new Client();
}
}