您的位置:首页 > 编程语言 > Java开发

java socket点对点以及点对面编程实例

2009-04-02 10:09 591 查看
java socket点对点以及点对面编程实例
和socket编程有关的几个类:
InetAddress
Socket:用在客户端
ServerSocket:用在服务器端
一。点对点通信
服务器端:
package server;

import java.io.*;
import java.net.*;

public class Server {
private int port;
public Server(int port){
this.port=port;
start();
}
//将从客户端收到的信息转化为大写的
public String process(String line){
return line.toUpperCase();
}
public void start(){
try{
//根据端口创建套接字
ServerSocket myscoket=new ServerSocket(port);
//显示连接信息
System.out.println("服务器启动完成,监听端口在"+port);
System.out.println("正在等待客户连接.........");
//挂起等待客户的请求
Socket connection=myscoket.accept();
//测试
System.out.println("客户发来连接请求.........");
//获取读取客户端的数据流
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
//获取写往客户端的数据输出流,true表示自动刷新
PrintWriter out=new PrintWriter(connection.getOutputStream(),true);
//向客户发送欢迎的信息
out.println("您好,服务器连接成功!");
out.println("输入bye断开与服务器的连接");
boolean done=false;
while(!done){
//读取客户端的内容
String line=in.readLine();
if(line==null){
done=true;
}else{
//从服务器端显示客户端发送的信息
System.out.println("从客户端来的内容"+line);
//信息处理
String message=process(line);
//向客户端发送信息
out.println("从服务器端口发送的信息"+message);
if(line.trim().equals("BYE"))
done=true;
}
}
//关闭通信
connection.close();
}catch(Exception e){
System.out.println(e);
}
}
}

package server;

public class ServerDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length!=1){
System.out.println("运行方式:java Server <端口号>");
return;
}
try{
//获得端口号
int port=Integer.parseInt(args[0]);
Server myserver=new Server(port);
}catch(Exception e){
System.out.println(e);
}
}

}

客户端:
package client;
import java.io.*;
import java.net.*;
public class Client {
private String host;
private int port;
public Client(String host,int port){
this.host=host;
this.port=port;
connect();
}
public void connect(){
try{
Socket connection;
if(host.equals("localhost"))
{connection=new Socket(InetAddress.getLocalHost(),port);}
else
{ connection=new Socket(InetAddress.getByName(host),port);}
//获得从键盘输入流
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
//获得服务器写内容的数据流
PrintWriter out=new PrintWriter(connection.getOutputStream(),true);
//获得接收服务器发送内容的输入流
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
//从服务器获得欢迎信息
System.out.println("服务器信息:"+in.readLine());
System.out.println("服务器信息:"+in.readLine());
//提示用户输入
System.out.print("请输入>");
boolean done=false;
while(!done){
//从键盘上读取字符
String line=stdin.readLine();
//发送到服务端
out.println(line);
//如果读到bye则结束循环
if(line.equalsIgnoreCase("bye"))
done=true;
//从服务器读取字符串
String info=in.readLine();
//显示从服务器发送来的数据
System.out.println("服务器信息:"+info);
//提示用户输入
if(!done)
System.out.print("请输入>");
}
//关闭
connection.close();
}catch(SecurityException e){
System.out.println("连接服务器出现安全问题!");
}catch(IOException e){
System.out.println("连接服务器出现I/O错误!");
}
}
}

package client;

public class ClientDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length!=2){
System.out.println("程序运行方式:java client <服务器名称><端口号>");
return;
}
String host=args[0];
try{
int port=Integer.parseInt(args[1]);
Client myserver=new Client(host,port);
}catch(Exception e){
System.out.println(e);
}
}

}
二。点对面通信
服务端:
package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server extends ServerSocket {
private int port;
//private Socket connection;
public Server(int port)throws IOException{
super(port);
this.port=port;
System.out.println("服务器启动完成,监听端口在"+port);
System.out.println("正在等待客户连接.....");
try{
while(true){
//挂起,直到客户请求
Socket connection=accept();
//建立服务线程
new ServerThread(connection,port);
}
}catch(IOException e){
System.out.println(e);
}finally{
close();
}
}
}
package server;

import java.io.*;
import java.net.*;

public class ServerThread extends Thread{
private int port;
private Socket connection;
private BufferedReader in;
private PrintWriter out;
public ServerThread(Socket s,int port)throws IOException{
this.port=port;
this.connection=s;
//获取读取客户端的数据流
in=new BufferedReader(new InputStreamReader(connection.getInputStream

(),"gb2312"));
//获取写往客户端的数据输出流,true表示自动刷新
out=new PrintWriter(connection.getOutputStream(),true);
//向客户发送欢迎的信息
out.println("您好,服务器连接成功!");
out.println("输入bye断开与服务器的连接");
//启动线程
start();
}
//将从客户端收到的信息转化为大写的
public String process(String line){
return line.toUpperCase();
}
public void run(){
try{
boolean done=false;
while(!done){
String line=in.readLine();
if(line==null)
done=true;
else{
if(line.trim().equals("bye"))
done=true;
System.out.println("从客户端来的内容"+line);
String message=process(line);
out.println("从服务器端口发出的内容"+message);

}
}
System.out.println("bye bye!");
//关闭通信
connection.close();
}catch(Exception e){
System.out.println(e);
}
}
}
package server;

public class ServerDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length!=1){
System.out.println("运行方式:java Server <端口号>");
return;
}
try{
//获得端口号
int port=Integer.parseInt(args[0]);
Server myserver=new Server(port);
}catch(Exception e){
System.out.println(e);
}
}

}
客户端:
package client;
import java.io.*;
import java.net.*;
public class Client {
private String host;
private int port;
public Client(String host,int port){
this.host=host;
this.port=port;
connect();
}
public void connect(){
try{
Socket connection;
if(host.equals("localhost"))
connection=new Socket(InetAddress.getLocalHost(),port);
else
connection=new Socket(InetAddress.getByName(host),port);
//获得从键盘输入流
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
//获得服务器写内容的数据流
PrintWriter out=new PrintWriter(connection.getOutputStream(),true);
//获得接收服务器发送内容的输入流
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
//从服务器获得欢迎信息
System.out.println("服务器信息:"+in.readLine());
System.out.println("服务器信息:"+in.readLine());
//提示用户输入
System.out.print("请输入>");
boolean done=false;
while(!done){
//从键盘上读取字符
String line=stdin.readLine();
//发送到服务端
out.println(line);
//如果读到bye则结束循环
if(line.equalsIgnoreCase("bye"))
done=true;
//从服务器读取字符串
String info=in.readLine();
//显示从服务器发送来的数据
System.out.println("服务器信息:"+info);
//提示用户输入
if(!done)
System.out.print("请输入>");
}
//关闭
connection.close();
}catch(SecurityException e){
System.out.println("连接服务器出现安全问题!");
}catch(IOException e){
System.out.println("连接服务器出现I/O错误!");
}
}
}
package client;

public class ClientDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length!=2){
System.out.println("程序运行方式:java client <服务器名称><端口号>");
return;
}
String host=args[0];
try{
int port=Integer.parseInt(args[1]);
Client myserver=new Client(host,port);
}catch(Exception e){
System.out.println(e);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: