11、地址的表示1、域名(www.baidu.com);2、IP地址(202.108.35.210);
Java程序设计实用教程(源码以及电子教案:www.ptpedu.com.cn)
二、用UDP实现张三和李四对话的例子
张三的代码:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class ZhangSan {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
Thread readData ;
ReceiveLetterForZhang receiver = new ReceiveLetterForZhang();
try{ readData = new Thread(receiver);
readData.start(); //负责接收信息的线程
byte [] buffer=new byte[1];
InetAddress address=InetAddress.getByName("127.0.0.1");
DatagramPacket dataPack=
new DatagramPacket(buffer,buffer.length, address,666);
DatagramSocket postman=new DatagramSocket();
System.out.print("输入发送给李四的信息:");
while(scanner.hasNext()) {
String mess = scanner.nextLine();
buffer=mess.getBytes();
if(mess.length()==0)
System.exit(0);
buffer=mess.getBytes();
dataPack.setData(buffer);
postman.send(dataPack);
System.out.print("继续输入发送给李四的信息:");
}
}
catch(Exception e) {
System.out.println(e);
}
}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveLetterForZhang implements Runnable{
public void run() {
DatagramPacket pack=null;
DatagramSocket postman=null;
byte data[]=new byte[8192];
try{ pack=new DatagramPacket(data,data.length);
postman = new DatagramSocket(888);
}
catch(Exception e){}
while(true) {
if(postman==null) break;
else
try{ postman.receive(pack);
String message=new String(pack.getData(),0,pack.getLength());
System.out.printf("%25s\n","收到:"+message);
}
catch(Exception e){}
}
}
}
李四的代码:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class LiSi {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
Thread readData ;
ReceiveLetterForLi receiver = new ReceiveLetterForLi();
try{ readData = new Thread(receiver);
readData.start(); //负责接收信息的线程
byte [] buffer=new byte[1];
InetAddress address=InetAddress.getByName("127.0.0.1");
DatagramPacket dataPack=
new DatagramPacket(buffer,buffer.length, address,888);
DatagramSocket postman=new DatagramSocket();
System.out.print("输入发送给张三的信息:");
while(scanner.hasNext()) {
String mess = scanner.nextLine();
buffer=mess.getBytes();
if(mess.length()==0)
System.exit(0);
buffer=mess.getBytes();
dataPack.setData(buffer);
postman.send(dataPack);
System.out.print("继续输入发送给张三的信息:");
}
}
catch(Exception e) {
System.out.println(e);
}
}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveLetterForLi implements Runnable {
public void run() {
DatagramPacket pack=null;
DatagramSocket postman=null;
byte data[]=new byte[8192];
try{ pack=new DatagramPacket(data,data.length);
postman = new DatagramSocket(666);
}
catch(Exception e){}
while(true) {
if(postman==null) break;
else
try{ postman.receive(pack);
String message=new String(pack.getData(),0,pack.getLength());
System.out.printf("%25s\n","收到:"+message);
}
catch(Exception e){}
}
}
}
三、用Socket实现客户端和服务器之间交互(计算圆的面积)
客户端代码:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Client {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
Socket mysocket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread readData ;
Read read=null;
try{ mysocket=new Socket();
read = new Read();
readData = new Thread(read);
System.out.print("输入服务器的IP:");
String IP = scanner.nextLine();
System.out.print("输入端口号:");
int port = scanner.nextInt();
if(mysocket.isConnected()){}
else{
InetAddress address=InetAddress.getByName(IP);
InetSocketAddress socketAddress=new InetSocketAddress(address,port);
mysocket.connect(socketAddress);
in =new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
read.setDataInputStream(in);
readData.start();
}
}
catch(Exception e) {
System.out.println("服务器已断开"+e);
}
System.out.print("输入园的半径(放弃请输入N):");
while(scanner.hasNext()) {
double radius=0;
try {
radius = scanner.nextDouble();
}
catch(InputMismatchException exp){
System.exit(0);
}
try {
out.writeDouble(radius);
}
catch(Exception e) {}
}
}
}
中间线程代码Read类:
import java.io.DataInputStream;
import java.io.IOException;
public class Read implements Runnable {
DataInputStream in;
public void setDataInputStream(DataInputStream in) {
this.in = in;
}
public void run() {
double result=0;
while(true) {
try{ result=in.readDouble();
System.out.println("圆的面积:"+result);
System.out.print("输入园的半径(放弃请输入N):");
}
catch(IOException e) {
System.out.println("与服务器已断开"+e);
break;
}
}
}
}
服务端代码:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
ServerSocket server=null;
ServerThread thread;
Socket you=null;
while(true) {
try{ server=new ServerSocket(2010);
}
catch(IOException e1) {
System.out.println("正在监听"); //ServerSocket对象不能重复创建
}
try{ System.out.println(" 等待客户呼叫");
you=server.accept();
System.out.println("客户的地址:"+you.getInetAddress());
}
catch (IOException e) {
System.out.println("正在等待客户");
}
if(you!=null) {
new ServerThread(you).start(); //为每个客户启动一个专门的线程
}
}
}
}
class ServerThread extends Thread {
Socket socket;
DataOutputStream out=null;
DataInputStream in=null;
String s=null;
ServerThread(Socket t) {
socket=t;
try { out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
}
catch (IOException e){}
}
public void run() {
while(true) {
try{ double r=in.readDouble();//堵塞状态,除非读取到信息
double area=Math.PI*r*r;
out.writeDouble(area);
}
catch (IOException e) {
System.out.println("客户离开");
return;
}
}
}
}