看完了《think in java》開(kāi)始中文翻譯的還行,到了后來(lái)就感覺(jué)有點(diǎn)亂了,干脆就看英文版的了。看完之后感觸頗深。java!有前途! 開(kāi)始學(xué)java網(wǎng)絡(luò)方面的東西時(shí),實(shí)在是無(wú)從下手,走了不少?gòu)澛?。畢竟是在沒(méi)有老師指導(dǎo)的情況下,網(wǎng)上文章多而不精,多是講理論的東西。可能是太簡(jiǎn)單的緣故,高手都不屑一顧了。不過(guò)經(jīng)過(guò)“不懈努力”還是給整明白了。 java網(wǎng)絡(luò)通訊可以由"java.rmi"包和"java.net"來(lái)實(shí)現(xiàn),rmi(remote method invocation)遠(yuǎn)程方法調(diào)用。如果想了解這兩個(gè)包可以查看java幫助文檔中的例子,本文就不做詳細(xì)說(shuō)明了。本文主要講"java.net"中的"socket"通信方法. 那么什么是socket呢?一個(gè)socket連結(jié)就相當(dāng)于一個(gè)生活中的電話連接。電話有電話號(hào)碼,那么socket有需要什么呢?一個(gè)是主機(jī)名或域名如(www.sina.com)或ip地址,另一個(gè)是端口。什么是端口?如果說(shuō)ip地址代表一個(gè)旅館,那么端口就是房間號(hào)。這回明白了吧。 傳統(tǒng)通信一般有服務(wù)端(server)和客戶端(client)不過(guò)現(xiàn)在這種差別正在越來(lái)越小,甚至沒(méi)有了c/s的差別。兩個(gè)人聊qq,雙方都是對(duì)等的。不過(guò)為方便起見(jiàn)還是按c/s講了。首先了解一下通訊的的基本模式。
服務(wù)端server ServerSocket listener = new ServerSocket(4700); //建立監(jiān)聽(tīng)對(duì)象listener,端口自便不過(guò)不要設(shè)在0-1023之間 Socket server = listener.accept(); //建立服務(wù)段對(duì)象 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//創(chuàng)建輸入輸出流 server.close();
客戶端client Socket client = new Socket("192.168.1.12",4700); //建立socket對(duì)象連接到192.168.1.12端口為4700的服務(wù)端(如果你只有一臺(tái)電腦那 //么可以把ip設(shè)為127.0.0.1 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//創(chuàng)建輸入輸出流 server.close();
好了客戶端和服務(wù)端都通過(guò)in和out就可以通信了詳細(xì)代碼: client: import java.io.*;
import java.net.*;
import ToolClass.*;//里面是一個(gè)輸入類用于測(cè)試
public class Client {
/** Creates a new instance of Client */
public Client() {
}
public static void main(String[] args){
System.out.println("this is client");
try{
String writetext=null;
String get=null;
Socket client = new Socket("192.168.1.12",4700);
while(writetext!="bye"){
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
writetext = Input.readString(); //send
PrintWriter pout = new PrintWriter(out,true);
pout.println(writetext);
BufferedReader bin = new BufferedReader(new InputStreamReader(in));//receive
get = bin.readLine();
System.out.println("client output:"+get);
}
client.close();
System.out.println("client:good bye");
}
catch(IOException e){
System.out.println("err");
}
}
} server: import java.io.*;
import java.net.*;
import ToolClass.*;
public class Server {
/** Creates a new instance of Server */
public Server() {
}
public static void main(String[] args){
System.out.println("this is server");
try{
ServerSocket listener = new ServerSocket(4700);
String receive=null;
String send=null;
Socket server = listener.accept();
while(receive!="bye"){
InputStream in = server.getInputStream();
OutputStream out = server.getOutputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));//receive
receive = bin.readLine();
System.out.println("server output:"+receive);
send = Input.readString(); //sendinfo
PrintWriter pout = new PrintWriter(out,true);
pout.println(send);
}
server.close();
listener.close();
System.out.println("server:bye");
}
catch(IOException e){
System.out.println("err");
}
}
}
Input類: package ToolClass;
import java.io.*; /**
*
* @author zsj1
*/
public class Input {
/** Creates a new instance of Input */
public Input() {
}
static InputStreamReader isr=new InputStreamReader(System.in);
static BufferedReader br=new BufferedReader(isr);
public static int readInt(){
int i=0;
try{
i=Integer.parseInt(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
return i;
}
public static float readFloat() {
float f=0.0f;
try{
f=Float.parseFloat(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
return f;
}
public static String readString() {
String s="";
try{
s=br.readLine();
}
catch(Exception e){
System.out.println(e);
}
return s;
}
public static double readDouble() {
double d=0.0d;
try{
d=Double.parseDouble(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
return d;
}
} 把三段代碼放在三個(gè).java文件里,并放在同一個(gè)文件夾下,就可以運(yùn)行測(cè)試了。你愿意的話還可以改成圖形界面的。有不明白的email我
服務(wù)端server ServerSocket listener = new ServerSocket(4700); //建立監(jiān)聽(tīng)對(duì)象listener,端口自便不過(guò)不要設(shè)在0-1023之間 Socket server = listener.accept(); //建立服務(wù)段對(duì)象 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//創(chuàng)建輸入輸出流 server.close();
客戶端client Socket client = new Socket("192.168.1.12",4700); //建立socket對(duì)象連接到192.168.1.12端口為4700的服務(wù)端(如果你只有一臺(tái)電腦那 //么可以把ip設(shè)為127.0.0.1 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//創(chuàng)建輸入輸出流 server.close();
好了客戶端和服務(wù)端都通過(guò)in和out就可以通信了詳細(xì)代碼: client: import java.io.*;
import java.net.*;
import ToolClass.*;//里面是一個(gè)輸入類用于測(cè)試
public class Client {
/** Creates a new instance of Client */
public Client() {
}
public static void main(String[] args){
System.out.println("this is client");
try{
String writetext=null;
String get=null;
Socket client = new Socket("192.168.1.12",4700);
while(writetext!="bye"){
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
writetext = Input.readString(); //send
PrintWriter pout = new PrintWriter(out,true);
pout.println(writetext);
BufferedReader bin = new BufferedReader(new InputStreamReader(in));//receive
get = bin.readLine();
System.out.println("client output:"+get);
}
client.close();
System.out.println("client:good bye");
}
catch(IOException e){
System.out.println("err");
}
}
} server: import java.io.*;
import java.net.*;
import ToolClass.*;
public class Server {
/** Creates a new instance of Server */
public Server() {
}
public static void main(String[] args){
System.out.println("this is server");
try{
ServerSocket listener = new ServerSocket(4700);
String receive=null;
String send=null;
Socket server = listener.accept();
while(receive!="bye"){
InputStream in = server.getInputStream();
OutputStream out = server.getOutputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));//receive
receive = bin.readLine();
System.out.println("server output:"+receive);
send = Input.readString(); //sendinfo
PrintWriter pout = new PrintWriter(out,true);
pout.println(send);
}
server.close();
listener.close();
System.out.println("server:bye");
}
catch(IOException e){
System.out.println("err");
}
}
}
Input類: package ToolClass;
import java.io.*; /**
*
* @author zsj1
*/
public class Input {
/** Creates a new instance of Input */
public Input() {
}
static InputStreamReader isr=new InputStreamReader(System.in);
static BufferedReader br=new BufferedReader(isr);
public static int readInt(){
int i=0;
try{
i=Integer.parseInt(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
return i;
}
public static float readFloat() {
float f=0.0f;
try{
f=Float.parseFloat(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
return f;
}
public static String readString() {
String s="";
try{
s=br.readLine();
}
catch(Exception e){
System.out.println(e);
}
return s;
}
public static double readDouble() {
double d=0.0d;
try{
d=Double.parseDouble(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
return d;
}
} 把三段代碼放在三個(gè).java文件里,并放在同一個(gè)文件夾下,就可以運(yùn)行測(cè)試了。你愿意的話還可以改成圖形界面的。有不明白的email我