JAVA SOCKET编程

来自:http://www.exampledepot.com/egs/java.net/pkg.html

e151. Sending a Datagram

public static void send(InetAddress dst, int port, byte[] outbuf, int len) {
try {
DatagramPacket request = new DatagramPacket(outbuf, len, dst, port);
DatagramSocket socket = new DatagramSocket();
socket.send(request);
} catch (SocketException e) {
} catch (IOException e) {
}
}

 

e152. Receiving a Datagram

try {
byte[] inbuf = new byte[256]; // default size
DatagramSocket socket = new DatagramSocket(); // Wait for packet
DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);

socket.receive(packet); // Data is now in inbuf
int numBytesReceived = packet.getLength();
}
 catch (SocketException e)
{ }
 catch (IOException e)
{ }
 
 

e147. Creating a Client Socket

// Create a socket without a timeout
try {
InetAddress addr = InetAddress.getByName( "java.sun.com");
 int port = 80; // This constructor will block until the connection succeeds
Socket socket = new Socket(addr, port); }
 catch (UnknownHostException e) { }
catch (IOException e) { }

 // Create a socket with a timeout
try {
InetAddress addr = InetAddress.getByName( "java.sun.com");
int port = 80;
SocketAddress sockaddr = new InetSocketAddress(addr, port); // Create an unbound socket
Socket sock = new Socket(); // This method will block no more than timeoutMs.
// If the timeout occurs, SocketTimeoutException is thrown.
 int timeoutMs = 2000; // 2 seconds
sock.connect(sockaddr, timeoutMs);
}
catch (UnknownHostException e)
{ }
catch (SocketTimeoutException e)
{ }
catch (IOException e)
{ }
 
 

e148. Creating a Server Socket

try { int port = 2000;
ServerSocket srv = new ServerSocket(port);

// Wait for connection from client.
Socket socket = srv.accept(); }
catch (IOException e)
 { }
 
 

e149. Reading Text from a Socket

try {
BufferedReader rd = new BufferedReader(new InputStreamReader( socket.getInputStream()));

String str;
while ((str = rd.readLine()) != null)
 {
     process(str);
 }

rd.close();
}
catch (IOException e)
{ }

e150. Writing Text to a Socket

try {
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter( socket.getOutputStream()));

wr.write( "aString");
wr.flush();
}
catch (IOException e)
 { }


e144. Getting the IP Address of a Hostname

try {
InetAddress addr = InetAddress.getByName("exampledepot.com");
byte[] ipAddr = addr.getAddress();

// Convert to dot representation
String ipAddrStr = "";
for (int i=0; i<ipAddr.length; i++) {
if (i > 0) {
ipAddrStr += ".";
}
ipAddrStr += ipAddr[i]&0xFF;
}
} catch (UnknownHostException e) {
}

e145. Getting the Hostname of an IP Address

This example attempts to retrieve the hostname for an IP address. Note that getHostName()may not succeed, in which case it simply returns the IP address.
try {
// Get hostname by textual representation of IP address
InetAddress addr = InetAddress.getByName("127.0.0.1");

// Get hostname by a byte array containing the IP address
byte[] ipAddr = new byte[]{127,0,0,1};
addr = InetAddress.getByAddress(ipAddr);

// Get the host name
String hostname = addr.getHostName();

// Get canonical host name
String hostnameCanonical = addr.getCanonicalHostName();
} catch (UnknownHostException e) {
}

e146. Getting the IP Address and Hostname of the Local Machine

try {
InetAddress addr = InetAddress.getLocalHost();

// Get IP Address
byte[] ipAddr = addr.getAddress();

// Get hostname
String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}

e153. Joining a Multicast Group

public void join(String groupName, int port) {
try {
MulticastSocket msocket = new MulticastSocket(port);
group = InetAddress.getByName(groupName);
msocket.joinGroup(group);
} catch (IOException e) {
}
}

e154. Receiving from a Multicast Group

Once you've created a multicast socket and joined the group, all datagrams sent to its corresponding multicast address will be available to be read from the socket. You can read from the socket just like you would from a unicast socket.
public void read(MulticastSocket msocket, byte[] inbuf) {
try {
DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);

// Wait for packet
msocket.receive(packet);

// Data is now in inbuf
int numBytesReceived = packet.getLength();
} catch (IOException e) {
}
}

e155. Sending to a Multicast Group

You can send to a multicast socket using either a DatagramSocketor a MulticastSocket.What makes it multicast is the address that is in the datagram. If the address is a multicast address, the datagram will reach the multicast members in the group. You only need to use MulticastSocketif you want to control the time-to-live of the datagram.
byte[] utbuf = new byte[1024];
int port =1234;
try {
DatagramSocket socket = new DatagramSocket();
InetAddress groupAddr = InetAddress.getByName("228.1.2.3");
DatagramPacket packet = new DatagramPacket(outbuf, outbuf.length, groupAddr, port);
socket.send(packet);
} catch (SocketException e) {
} catch (IOException e) {
}

你可能感兴趣的:(java,socket,String,byte,Constructor,DST)