哪位大虾有数据报Socket客户端例子的java源码?急……

alphazhao 2002-04-05 08:25:40
只要一个客户端的就可,能否发信息及随时接收信息即可。
高分求救
...全文
29 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
GJA106 2002-04-05
  • 打赏
  • 举报
回复
UDPClient端:
import java.net.*;
import java.io.*;

/**
* This class allows you to send and receive Strings and byte arrays via UDP
* without concerning yourself with DatagramPackets and DatagramSockets.
* @version 1.0 of June 1, 1996
* @author Elliotte Rusty Harold
*/
public class UDPClient {

InetAddress ia;
int port;
DatagramSocket ds;

/**
* Creates a new UDPClient.
* @param ia The InetAddress of the remote host to which data will be sent
* @param port The port on the remote host to which data will be sent
* @throws SocketException
*/
public UDPClient(InetAddress ia, int port) throws SocketException {

this.ia = ia;
this.port = port;
ds = new DatagramSocket();

}

public UDPClient(String hostname, int port) throws UnknownHostException, SocketException {

this(InetAddress.getByName(hostname), port);

}

/**
* This method sends data to the remote host via UDP. If the byte is longer than
* the maximum reliable length of a UDP Datagram (64900) bytes then an
* IOException is thrown
* @param buffer A byte array containing the data to be sent
* @throws IOException
*/
public void send(byte[] buffer) throws IOException {

if (buffer.length > 64900) throw new IOException();
DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, port);
ds.send(dp);
System.out.println("send(byte[] buffer) ");

}

/**
*
* This method sends an ISO-Latin-1 string to the remote host via UDP.
* The string will be truncated to ISO-Latin-1 even if it's Unicode.
* @param s The string to be sent
* @throws IOException
*/
public void send(String s) throws IOException
{
System.out.println("send(String s) throws IOException ");
byte[] data = new byte[s.length()];
s.getBytes(0, s.length(), data, 0);
send(data);

}

/**
* This method sends an empty datagram to the remote host via UDP.
* @throws IOException
*/
public void send() throws IOException {

byte[] b = new byte[1];
send(b);

}

/**
* This method blocks until a UDP Datagram is received from the host with which
* this UDPClient communicates. This can be an indefinite amount of time if
* the host is unreachable so calls to this method should be placed in a separate
* thread from the main program.
* @return the data received as a byte array
* @throws IOException
*/
public synchronized byte[] receive() throws IOException {

byte[] buffer = new byte[65507];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
ds.receive(incoming);
// a client should only receive data from the host to
while ( !incoming.getAddress().equals(ia)) {
ds.receive(incoming);
}
return incoming.getData();

}

/**
* This method blocks until a UDP Datagram is received from the host with which
* this UDPClient communicates. This can be an indefinite amount of time if
* the host is unreachable so calls to this method should be placed in a separate
* thread from the main program. When data is received it is
* converted into an ISO-latin-1 String and returned.
* @return the data received as a byte array
* @throws IOException
*/
public synchronized String receiveString() throws IOException {

byte[] data = receive();
return new String(data, 0, 0, data.length);

}

/**
* @return the port which this object sends data to
*/
public int getPort() {

return port;

}

/**
* @return the port which this client is bound to
*/
public int getLocalPort() {

return ds.getLocalPort();

}

/**
* @return the InetAddress which this client sends data to
*/
public InetAddress getAddress() {

return ia;

}

/**
* @return a String showning the remote host and port which this client sends data to
*/
public String toString() {

return ia + ":" + port;

}

public static void main(String args[])
{
try
{
InetAddress ia = InetAddress.getByName("ganja.saili.com");
System.out.println("ia:" + ia.getHostAddress() +":"+ia.getHostName());
UDPClient uu = new UDPClient(ia,30000);
uu.send(args[0]);
}
catch(Exception e)
{
}
}

}
GJA106 2002-04-05
  • 打赏
  • 举报
回复
UPDClient端:
import java.net.*;
import java.io.*;

/**
* This class allows you to send and receive Strings and byte arrays via UDP
* without concerning yourself with DatagramPackets and DatagramSockets.
* @version 1.0 of June 1, 1996
* @author Elliotte Rusty Harold
*/
public class UDPClient {

InetAddress ia;
int port;
DatagramSocket ds;

/**
* Creates a new UDPClient.
* @param ia The InetAddress of the remote host to which data will be sent
* @param port The port on the remote host to which data will be sent
* @throws SocketException
*/
public UDPClient(InetAddress ia, int port) throws SocketException {

this.ia = ia;
this.port = port;
ds = new DatagramSocket();

}

public UDPClient(String hostname, int port) throws UnknownHostException, SocketException {

this(InetAddress.getByName(hostname), port);

}

/**
* This method sends data to the remote host via UDP. If the byte is longer than
* the maximum reliable length of a UDP Datagram (64900) bytes then an
* IOException is thrown
* @param buffer A byte array containing the data to be sent
* @throws IOException
*/
public void send(byte[] buffer) throws IOException {

if (buffer.length > 64900) throw new IOException();
DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, port);
ds.send(dp);
System.out.println("send(byte[] buffer) ");

}

/**
*
* This method sends an ISO-Latin-1 string to the remote host via UDP.
* The string will be truncated to ISO-Latin-1 even if it's Unicode.
* @param s The string to be sent
* @throws IOException
*/
public void send(String s) throws IOException
{
System.out.println("send(String s) throws IOException ");
byte[] data = new byte[s.length()];
s.getBytes(0, s.length(), data, 0);
send(data);

}

/**
* This method sends an empty datagram to the remote host via UDP.
* @throws IOException
*/
public void send() throws IOException {

byte[] b = new byte[1];
send(b);

}

/**
* This method blocks until a UDP Datagram is received from the host with which
* this UDPClient communicates. This can be an indefinite amount of time if
* the host is unreachable so calls to this method should be placed in a separate
* thread from the main program.
* @return the data received as a byte array
* @throws IOException
*/
public synchronized byte[] receive() throws IOException {

byte[] buffer = new byte[65507];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
ds.receive(incoming);
// a client should only receive data from the host to
while ( !incoming.getAddress().equals(ia)) {
ds.receive(incoming);
}
return incoming.getData();

}

/**
* This method blocks until a UDP Datagram is received from the host with which
* this UDPClient communicates. This can be an indefinite amount of time if
* the host is unreachable so calls to this method should be placed in a separate
* thread from the main program. When data is received it is
* converted into an ISO-latin-1 String and returned.
* @return the data received as a byte array
* @throws IOException
*/
public synchronized String receiveString() throws IOException {

byte[] data = receive();
return new String(data, 0, 0, data.length);

}

/**
* @return the port which this object sends data to
*/
public int getPort() {

return port;

}

/**
* @return the port which this client is bound to
*/
public int getLocalPort() {

return ds.getLocalPort();

}

/**
* @return the InetAddress which this client sends data to
*/
public InetAddress getAddress() {

return ia;

}

/**
* @return a String showning the remote host and port which this client sends data to
*/
public String toString() {

return ia + ":" + port;

}

public static void main(String args[])
{
try
{
InetAddress ia = InetAddress.getByName("ganja.saili.com");
System.out.println("ia:" + ia.getHostAddress() +":"+ia.getHostName());
UDPClient uu = new UDPClient(ia,30000);
uu.send(args[0]);
}
catch(Exception e)
{
}
}

}
GJA106 2002-04-05
  • 打赏
  • 举报
回复
别人的例子:
import java.net.*;
import java.io.*;

public class UDPServer
{
protected static int defaultPort = 0;
protected static int defaultBufferLength = 65507;

public static void main(String[] args)
{
DatagramPacket incoming;

int port;
int len;

try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
port = defaultPort;
}
try {
len = Integer.parseInt(args[1]);
}
catch (Exception e) {
len = defaultBufferLength;
}

try {
DatagramSocket ds = new DatagramSocket(port);
byte[] buffer = new byte[len];
while (true)
{
incoming = new DatagramPacket(buffer, buffer.length);
try
{
ds.receive(incoming);
respond(ds, incoming);
}
catch (IOException e) {
System.err.println(e);
}
} // end while
} // end try
catch (SocketException se) {
System.err.println(se);
} // end catch

} // end main

public static void respond(DatagramSocket ds, DatagramPacket dp) {
;
}

}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧