Java中的网络编程类
n Java.net包
– TCP协议
URL
URLConnection
Socket
ServerSocket
– UDP协议
DatagramPacket
DatagramSocket
MulticastSocket
TCP Socket客户端
package com.cauc.edu;
import java.net.*;
import java.io.*;
import java.util.*;
/**
* 主要步骤:
* 1.设置连接的host及端口
* 2.获取输入输出流
* 3.从输入流中读取
* 4.从输出流中写入
* @author JMZHANG
*
*/
public class Client {
private String host="localhost";
private int port=8000;
private Socket socket;
public Client()throws IOException{
//设置连接的host及端口
socket=new Socket(host,port);
}
public static void main(String args[])throws IOException{
new Client().talk();
}
private PrintWriter getWriter(Socket socket)throws IOException{
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket)throws IOException{
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public void talk()throws IOException {
try{
//获取输入输出流
BufferedReader br=getReader(socket);
PrintWriter pw=getWriter(socket);
//从输入流中读取
BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in));
String msg=null;
while((msg=localReader.readLine())!=null){
//从输出流中写入
pw.println(msg);
System.out.println(br.readLine());
if(msg.equals("bye"))
break;
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{socket.close();}catch(IOException e){e.printStackTrace();}
}
}
}
TCP Socket服务器端
package com.cauc.edu;
import java.io.*;
import java.net.*;
/**
* 主要步骤:
* 1.绑定端口
* 2.接受客户端连接
* 3.获取输入输出流
* 4.从输入流中读取
* 5.从输出流中写入
* @author JMZHANG
*
*/
public class Server {
private int port=8000;
private ServerSocket serverSocket;
public Server() throws IOException {
//绑定端口
serverSocket = new ServerSocket(port);
System.out.println("服务器启动");
}
public void service() {
while (true) {
Socket socket=null;
try {
//接受客户端连接
socket = serverSocket.accept();
//创建一个工作线程
Thread workThread=new Thread(new Handler(socket));
//启动工作线程
workThread.start();
}catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[])throws IOException {
new Server().service();
}
}
class Handler implements Runnable{
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
}
private PrintWriter getWriter(Socket socket)throws IOException{
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket)throws IOException{
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public String echo(String msg) {
return "echo:" + msg;
}
public void run(){
try {
System.out.println("New connection accepted " +
socket.getInetAddress() + ":" +socket.getPort());
//获取输入输出流
BufferedReader br =getReader(socket);
PrintWriter pw = getWriter(socket);
String msg = null;
//从输入流中读取
while ((msg = br.readLine()) != null) {
System.out.println(msg);
//从输出流中写入
pw.println(echo(msg));
if (msg.equals("bye"))
break;
}
}catch (IOException e) {
e.printStackTrace();
}finally {
try{
if(socket!=null)socket.close();
}catch (IOException e) {e.printStackTrace();}
}
}
}
UDP 客户端:
package com.cauc.edu;
import java.io.*;
import java.net.*;
/**
* 主要步骤:
* 1.创建DatagramSocket,并绑定端口
* 2.通过DatagramPacket读取输入,写入输出
* @author JMZHANG
*
*/
public class udpServer {
private int port=8000;
private DatagramSocket socket;
public udpServer() throws IOException {
// 创建DatagramSocket,并绑定端口
socket=new DatagramSocket(port);
System.out.println("服务器启动");
}
public String echo(String msg) {
return "echo:" + msg;
}
public void service() {
while (true) {
try {
//通过DatagramPacket读取输入,写入输出
DatagramPacket packet=new DatagramPacket(new byte[512],512);
socket.receive(packet);
String msg=new String(packet.getData(),0,packet.getLength());
System.out.println(packet.getAddress() + ":" +packet.getPort()+">"+msg);
packet.setData(echo(msg).getBytes());
socket.send(packet);
}catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[])throws IOException {
new udpServer().service();
}
}
UDP服务器端:
package com.cauc.edu;
import java.net.*;
import java.io.*;
/**
* 主要步骤
* 1.创建DatagramSocket
* 2.获取服务器地址
* 3.创建需要发送的内容(DatagramPacket),同时设置地址及端口
* 4.通过DatagramSocket发送DatagramPacket
? 注意
* UDP通讯是不可靠的,因此可能丢包
* UDP通讯速度很快
* @author JMZHANG
*
*/
public class udpClient {
private String remoteHost="localhost";
private int remotePort=8000;
private DatagramSocket socket;
public udpClient()throws IOException{
//创建DatagramSocket,与本地的任意一个UDP端口绑定
socket=new DatagramSocket();
}
public static void main(String args[])throws IOException{
new udpClient().talk();
}
public void talk()throws IOException {
try{
//获取服务器地址
InetAddress remoteIP=InetAddress.getByName(remoteHost);
BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in));
String msg=null;
while((msg=localReader.readLine())!=null){
byte[] outputData=msg.getBytes();
//创建需要发送的内容(DatagramPacket),同时设置地址及端口
DatagramPacket outputPacket=new DatagramPacket(outputData,outputData.length,remoteIP,remotePort);
//通过DatagramSocket发送DatagramPacket
socket.send(outputPacket);
DatagramPacket inputPacket=new DatagramPacket(new byte[512],512);
socket.receive(inputPacket);
System.out.println(new String(inputPacket.getData(),0,inputPacket.getLength()));
if(msg.equals("bye"))
break;
}
}catch(IOException e){
e.printStackTrace();
}finally{
socket.close();
}
}
}