看完了《think in java》開始中文翻譯的還行,到了后來就感覺有點(diǎn)亂了,干脆就看英文版的了??赐曛蟾杏|頗深。java!有前途! 開始學(xué)java網(wǎng)絡(luò)方面的東西時,實(shí)在是無從下手,走了不少彎路。畢竟是在沒有老師指導(dǎo)的情況下,網(wǎng)上文章多而不精,多是講理論的東西。可能是太簡單的緣故,高手都不屑一顧了。不過經(jīng)過“不懈努力”還是給整明白了。 java網(wǎng)絡(luò)通訊可以由"java.rmi"包和"java.net"來實(shí)現(xiàn),rmi(remote method invocation)遠(yuǎn)程方法調(diào)用。如果想了解這兩個包可以查看java幫助文檔中的例子,本文就不做詳細(xì)說明了。本文主要講"java.net"中的"socket"通信方法. 那么什么是socket呢?一個socket連結(jié)就相當(dāng)于一個生活中的電話連接。電話有電話號碼,那么socket有需要什么呢?一個是主機(jī)名或域名如(www.sina.com)或ip地址,另一個是端口。什么是端口?如果說ip地址代表一個旅館,那么端口就是房間號。這回明白了吧。 傳統(tǒng)通信一般有服務(wù)端(server)和客戶端(client)不過現(xiàn)在這種差別正在越來越小,甚至沒有了c/s的差別。兩個人聊qq,雙方都是對等的。不過為方便起見還是按c/s講了。首先了解一下通訊的的基本模式。
服務(wù)端server ServerSocket listener = new ServerSocket(4700); //建立監(jiān)聽對象listener,端口自便不過不要設(shè)在0-1023之間 Socket server = listener.accept(); //建立服務(wù)段對象 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//創(chuàng)建輸入輸出流 server.close();
客戶端client Socket client = new Socket("192.168.1.12",4700); //建立socket對象連接到192.168.1.12端口為4700的服務(wù)端(如果你只有一臺電腦那 //么可以把ip設(shè)為127.0.0.1 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//創(chuàng)建輸入輸出流 server.close();
好了客戶端和服務(wù)端都通過in和out就可以通信了詳細(xì)代碼: client: import java.io.*;
import java.net.*;
import ToolClass.*;//里面是一個輸入類用于測試
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;
}
} 把三段代碼放在三個.java文件里,并放在同一個文件夾下,就可以運(yùn)行測試了。你愿意的話還可以改成圖形界面的。有不明白的email我
服務(wù)端server ServerSocket listener = new ServerSocket(4700); //建立監(jiān)聽對象listener,端口自便不過不要設(shè)在0-1023之間 Socket server = listener.accept(); //建立服務(wù)段對象 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//創(chuàng)建輸入輸出流 server.close();
客戶端client Socket client = new Socket("192.168.1.12",4700); //建立socket對象連接到192.168.1.12端口為4700的服務(wù)端(如果你只有一臺電腦那 //么可以把ip設(shè)為127.0.0.1 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//創(chuàng)建輸入輸出流 server.close();
好了客戶端和服務(wù)端都通過in和out就可以通信了詳細(xì)代碼: client: import java.io.*;
import java.net.*;
import ToolClass.*;//里面是一個輸入類用于測試
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;
}
} 把三段代碼放在三個.java文件里,并放在同一個文件夾下,就可以運(yùn)行測試了。你愿意的話還可以改成圖形界面的。有不明白的email我