看完了《think in java》开始中文翻译的还行,到了后来就感觉有点乱了,干脆就看英文版的了。看完之后感触颇深。java!有前途! 开始学java网络方面的东西时,实在是无从下手,走了不少弯路。毕竟是在没有老师指导的情况下,网上文章多而不精,多是讲理论的东西。可能是太简单的缘故,高手都不屑一顾了。不过经过“不懈努力”还是给整明白了。 java网络通讯可以由"java.rmi"包和"java.net"来实现,rmi(remote method invocation)远程方法调用。如果想了解这两个包可以查看java帮助文档中的例子,本文就不做详细说明了。本文主要讲"java.net"中的"socket"通信方法. 那么什么是socket呢?一个socket连结就相当于一个生活中的电话连接。电话有电话号码,那么socket有需要什么呢?一个是主机名或域名如(www.sina.com)或ip地址,另一个是端口。什么是端口?如果说ip地址代表一个旅馆,那么端口就是房间号。这回明白了吧。 传统通信一般有服务端(server)和客户端(client)不过现在这种差别正在越来越小,甚至没有了c/s的差别。两个人聊qq,双方都是对等的。不过为方便起见还是按c/s讲了。首先了解一下通讯的的基本模式。
服务端server ServerSocket listener = new ServerSocket(4700); //建立监听对象listener,端口自便不过不要设在0-1023之间 Socket server = listener.accept(); //建立服务段对象 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//创建输入输出流 server.close();
客户端client Socket client = new Socket("192.168.1.12",4700); //建立socket对象连接到192.168.1.12端口为4700的服务端(如果你只有一台电脑那 //么可以把ip设为127.0.0.1 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//创建输入输出流 server.close();
好了客户端和服务端都通过in和out就可以通信了详细代码: 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文件里,并放在同一个文件夹下,就可以运行测试了。你愿意的话还可以改成图形界面的。有不明白的email我
服务端server ServerSocket listener = new ServerSocket(4700); //建立监听对象listener,端口自便不过不要设在0-1023之间 Socket server = listener.accept(); //建立服务段对象 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//创建输入输出流 server.close();
客户端client Socket client = new Socket("192.168.1.12",4700); //建立socket对象连接到192.168.1.12端口为4700的服务端(如果你只有一台电脑那 //么可以把ip设为127.0.0.1 InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();//创建输入输出流 server.close();
好了客户端和服务端都通过in和out就可以通信了详细代码: 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文件里,并放在同一个文件夹下,就可以运行测试了。你愿意的话还可以改成图形界面的。有不明白的email我