基于sokcet服务器实现:
通过握手形式建立了端到端的连接,首先客户端向服务器发起申请,之后服务器反馈处理信息,最后客户端发送确认信息。
基于(单和多)线程soket的客户机代码是不变的
服务器代码(非多线程)
package ht;
import java.io.*;
import java.net.*;
public class TalkSevere{
public static void main(String[] args) {
try {
ServerSocket server =null;
try {
server=new ServerSocket(4700);//端口设定
}
catch(Exception e) {
System.out.println("can not listen to :"+e);
}
Socket socket=null;
try {
socket=server.accept();//在端口对连接进行监听
}
catch(Exception e) {
System.out.println("Error"+e);
}
String line;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os= new PrintWriter(socket.getOutputStream());
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client:"+is.readLine());
line=sin.readLine();
while(!(line.equals("bye"))) {
os.println(line);
os.flush();
System.out.println("Sever:"+line);
System.out.println("Client:"+is.readLine());
line=sin.readLine();
}
os.close();
is.close();
server.close();
socket.close();
}
catch(Exception e) {
System.out.println("Error"+e);
}
}
}
服务器代码(多线程):
//多线程服务器线程程序
package ht;
import java.io.*;
import java.net.*;
public class ServerThread extends Thread {
Socket socket=null; //创建空socket引用用于接收socket
int clientnum; //连接客户机数目
public ServerThread(Socket socket,int num){
this.socket=socket;
clientnum=num+1;
}
public void run(){//线程体
try {
String line ;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os= new PrintWriter(socket.getOutputStream());
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client:"+clientnum+is.readLine());
line=sin.readLine();
while(!line.equals("bye")) {
os.println(line);
os.flush();
System.out.println("Server:"+line);
System.out.println(""+clientnum+is.readLine());
line=sin.readLine();
}
os.close();
is.close();
socket.close();
}
catch(Exception e) {
}
}
}
客户机实现:
package ht;
import java.io.*;
import java.net.*;
public class TalkClient{
public static void main(String[] args) {
try {
Socket socket=new Socket("127.0.0.1",4700);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os =new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String rl;
rl=sin.readLine();
while(!(rl.equals("bye"))) {
os.println(rl);
os.flush();
System.out.println("Client:"+rl);
System.out.println("Severe:"+is.readLine());
rl=sin.readLine();//从键盘键入系统缓冲区读取数据
}
os.close();
is.close();
socket.close();
}
catch(Exception e){
System.out.println("can not listen to:"+e);
}
}
}
//多线程服务器主程序
package zz;
import java.io.*;
import java.net.*;
import ht.ServerThread;
public class MultiTalkSever{
static int clientnum=0;//连接客户机个数
public static void main(String[] args) throws IOException{
ServerSocket serverSocket=null;
boolean listening=true;//监听区布尔值
try {
serverSocket=new ServerSocket(4700);//服务器插座对象创建
}
catch(IOException e) {
System.out.println("Could not listen on port:4700");
System.exit(-1);//无连接时输出提示并退出
}
while(listening) {
new ServerThread(serverSocket.accept(),clientnum).start();//使用接收的socket创建线程对象,并执行对象run方法
clientnum++;
}
serverSocket.close();//关闭服务器对象
}
}
数据报实现的通讯程序:
基于数据报的低层次网络通信由于未建立连接,所以客户端到服务器端的通信是不可靠的传输。
客户端代码:
package jkl;
import java.net.*;
import java.io.*;
import java.util.*;
public class QuoteClient{
public static void main(String[] args) throws IOException {
//如果启动时没有给出Sever的名字,那么输出错误信息并退出
if(args.length!=1) {
System.out.println("Usage : java QuoteClient <hostname>");
return ;
}
DatagramSocket socket=new DatagramSocket();
byte[] buf=new byte[256];//缓冲区
//获得本机服务器IP address
InetAddress address=InetAddress.getByName(args[0]);
DatagramPacket packet= new DatagramPacket(buf,buf.length,address,4445);
socket.send(packet);
packet= new DatagramPacket(buf,buf.length);
socket.receive(packet);
String received =new String(packet.getData());
System.out.println("Quote of the Moment :"+received);
socket.close();
//单次数据发送
}
服务器主程序:
package jkl;
import java.net.*;
import java.io.*;
import java.util.*;
public class QuoteSever{
public static void main(String[] args) throws java.io.IOException{
new QuoteThread().start();
}
}
服务器线程程序:
package jkl;
import java.net.*;
import java.io.*;
import java.util.*;
public class QuoteThread extends Thread{
protected DatagramSocket socket =null;//数据报插座
protected BufferedReader in=null;
protected boolean moreQuotes=true;
public QuoteThread() throws IOException{
this("QuoteThread");
}
public QuoteThread(String name) throws IOException{
super(name);
socket =new DatagramSocket(4445);//服务器自身端口定义
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
}
public void run() {
//只有moreQuote=false时关闭服务器程序,即收到来自客户的“bye”
while(moreQuotes) {
try {
byte[] buf=new byte[256];//缓冲区创建
DatagramPacket packet=new DatagramPacket(buf,buf.length);
socket.receive(packet);
System.out.println(new String(packet.getData()));
String dString =in.readLine();
if(dString.equals("bye")) {moreQuotes=false;}
buf=dString.getBytes();//转换String为字节数组,以便传送
//从获取的数据报中读取发送方的信息,因为数据报是完整独立的信息段
InetAddress address =packet.getAddress();
int port =packet.getPort();
packet =new DatagramPacket(buf,buf.length,address,port);
socket.send(packet);
}
catch(Exception e) {
e.printStackTrace();
moreQuotes=false;
}
}
}
}