網絡編程中最重要的就是SOCKET,它其實也就是監聽端口的原理。和我們用手機發短信的原理應該是大致無二(我是這樣理解的),而JAVA最出色的一點也就是“無痛苦連網”。
網絡最基本的精神就是讓兩台機器連接起來,“被呼叫的一方”也就是服務器,而“找人的一方”則叫做客戶機,所以說在連接中服務器、客戶機也就是一個相對的概念了。而我們對機器的標識主要是通過IP地址和端口來區分的。
“傳輸控制協議”TCP和“用戶數據報協議”是兩種不同的協議,JAVA對這兩種協議的支持基本是一致的,而它們本身最大的區別也就是發送的可靠性和速率,前者相比後者是可靠協議,後者當然是速度快得多了,下面我們分別用兩個SOCKET下演示:
eg1:
//Clients.java
import java.io.*;
import java.net.*;
public class Clients
{
public static void main(String[] args) throws Exception
{
InetAddress addr = InetAddress.getByName(null);
Socket socket = new Socket(addr,2000);
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
byte[] b = new byte[2048];
String msg = new String(b,0,System.in.read(b));
out.println(msg);
socket.close();
}
}
//Servers.java
import java.io.*;
import java.net.*;
public class Servers
{
public static void main(String[] args) throws Exception
{
ServerSocket s = new ServerSocket(2000);
try{
while(true){
Socket socket = s.accept();
try{
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
StringBuffer sb = new StringBuffer();
int c;
while( (c = in.read()) != -1 ){
char ch = (char)c;
sb.append(ch);
}
System.out.println(sb.toString());
}catch(IOException e){
socket.close();
}finally{
socket.close();
}
}//while
}finally{
s.close();
}//try
}//main
}
此程式主要用Servers來進行無限監聽,而Clients是客戶機發送程式,他們的端口全采用2000。
eg2:
//UDPsend.java
import java.io.*;
import java.net.*;
/**
* This class sends the specified text or file as a datagram to the
* specified port of the specified host.
**/
public class UDPSend {
public static final String usage =
"Usage: java UDPSend ...\n" +
" or: java UDPSend -f ";
public static void main(String args[]) {
try {
// Check the number of arguments
if (args.length < 3)
throw new IllegalArgumentException("Wrong number of args");
// Parse the arguments
String host = args[0];
int port = Integer.parseInt(args[1]);
// Figure out the message to send.
// If the third argument is -f, then send the contents of the file
// specified as the fourth argument. Otherwise, concatenate the
// third and all remaining arguments and send that.
byte[] message;
if (args[2].equals("-f")) {
File f = new File(args[3]);
int len = (int)f.length(); // figure out how big the file is
message = new byte[len]; // create a buffer big enough
FileInputStream in = new FileInputStream(f);
int bytes_read = 0, n;
do { // loop until we've read it all
n = in.read(message, bytes_read, len-bytes_read);
bytes_read += n;
} while((bytes_read < len)&& (n != -1));
}
else { // Otherwise, just combine all the remaining arguments.
String msg = args[2];
for (int i = 3; i < args.length; i++) msg += " " + args[i];
message = msg.getBytes();
}
// Get the internet address of the specified host
InetAddress address = InetAddress.getByName(host);
// Initialize a datagram packet with data and address
DatagramPacket packet = new DatagramPacket(message, message.length,
address, port);
// Create a datagram socket, send the packet through it, close it.
DatagramSocket dsocket = new DatagramSocket();
dsocket.send(packet);
dsocket.close();
}
catch (Exception e) {
System.err.println(e);
System.err.println(usage);
}
}
}
//UDPreceive.java
import java.io.*;
import java.net.*;
/**
* This program waits to receive datagrams sent the specified port.
* When it receives one, it displays the sending host and prints the
* contents of the datagram as a string. Then it loops and waits again.
**/
public class UDPReceive {
public static final String usage = "Usage: java UDPReceive ";
public static void main(String args[]) {
try {
if (args.length != 1)
throw new IllegalArgumentException("Wrong number of args");
// Get the port from the command line
int port = Integer.parseInt(args[0]);
// Create a socket to listen on the port.
DatagramSocket dsocket = new DatagramSocket(port);
// Create a buffer to read datagrams into. If anyone sends us a
// packet containing more than will fit into this buffer, the
// excess will simply be discarded!
byte[] buffer = new byte[2048];
// Create a packet to receive data into the buffer
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Now loop forever, waiting to receive packets and printing them.
for(;;) {
// Wait to receive a datagram
dsocket.receive(packet);
// Convert the contents to a string, and display them
String msg = new String(buffer, 0, packet.getLength());
System.out.println(packet.getAddress().getHostName() +
": " + msg);
// Reset the length of the packet before reusing it.
// Prior to Java 1.1, we'd just create a new packet each time.
packet.setLength(buffer.length);
}
}
catch (Exception e) {
System.err.println(e);
System.err.println(usage);
}
}
}
在UDP中主要的類是DatagramSocket()和DatagramPacket(),而在UDPreceive中,被接受的字節是受限制,這些感覺不是太好,既然buf是一個字節數組,我們實在是很奇怪為什麼構建器自己不能調查出數組的長度呢?唯一能猜測的原因就是C風格的編程使然,那裡的數組不能自己告訴我們它有多大。
而我們實際使用的過程中,當然不僅僅限於這些,其中要考慮有多台客戶機來連接服務器,所以要考慮到線程Thread的使用,如果再加上SWING,就可以做一個類似於QQ的SOCKET功能了,這僅僅限於我在學習SOCKET時的一些領悟。供大家參考。