java udp 和 tcp_java udp与tcp

一:基础  NET基本对象java.net.InetAddress类的使用

IP地址是IP使用的32位(IPv4)或者128位(IPv6)位无符号数字,它是传输层协议TCP,UDP的基础。InetAddress是Java 对IP地址的封装,在java.net中有许多类都使用到了InetAddress,包括 ServerSocket,Socket,DatagramSocket等等。

InetAddress的实例对象包含以数字形式保存的

IP地址,同时还可能包含主机名(如果使用主机名来获取InetAddress的实例,或者使用数字来构造,并且启用了反向主机名解析的功能)。

InetAddress类提供了将主机名解析为IP地址(或反之)的方法。

InetAddress的构造函数不是公开的(public),所以需要通过它提供的静态方法来获取,有以下的方法:

static InetAddress[] getAllByName(String host)

static InetAddress getByAddress(byte[] addr)

static InetAddress getByAddress(String host,byte[] addr)

static InetAddress getByName(String host)

static InetAddress getLocalHost()

实例代码:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

import java.net.InetAddress;

import java.net.UnknownHostException;public classDemo {/**

* @param args

* @throws UnknownHostException*/

public static voidmain(String[] args) throws UnknownHostException {//获取本地主机的IP地址对象//InetAddress inet = InetAddress.getLocalHost();//获取任意一台主机的IP地址对象

InetAddress inet = InetAddress.getByName("10.2.156.26");//获取本地主机的ip地址

String ip =inet.getHostAddress();//获取本机主机的主机名

String name =inet.getHostName();

System.out.println(ip+":"+name);

}

}

View Code

二:udp相关代码

1:使用udp协议实现数据的发送

1:创建Socket发送端

2:明确要发送的数据

3:把要发送的数据封装成数据报包

4:使用Socket的发送功能发送数据

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetAddress;importjava.net.SocketException;importjava.net.UnknownHostException;public classSend1 {/*** 使用udp协议实现数据的发送

* 1:创建Socket发送端

* 2:明确要发送的数据

* 3:把要发送的数据封装成数据报包

* 4:使用Socket的发送功能发送数据

*@throwsIOException*/

public static void main(String[] args) throwsIOException {

System.out.println("发送方启动......");//1:创建Socket发送端---确定了使用的协议

DatagramSocket ds = newDatagramSocket();//2:明确要发送的数据

String data = "明天放假";byte[] b =data.getBytes();//3:把要发送的数据封装成数据报包//数据报包中包含了要发送的数据,接收数据的主机的IP地址对象,以及接收方使用哪个端口来接收

DatagramPacket dp = new DatagramPacket(b,b.length,InetAddress.getByName("10.2.156.15"),22222);//4:使用Socket的发送功能发送数据//该方法内部使用的输出流,向网络上的另一台主机输出数据

ds.send(dp);

ds.close();

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.SocketException;public classReceive1 {/*** 使用udp协议实现数据的接收

* 1:创建Socket端点,同时要监听一个端口

* 2:创建用来接收数据的数据报包

* 3:使用Socket的接收功能来接收数据

*@throwsIOException*/

public static void main(String[] args) throwsIOException {

System.out.println("接收方启动......");//1:创建Socket端点

DatagramSocket ds = new DatagramSocket(22222);//2:创建一个空的数据报包来接收发送过来的数据报包

byte[] arr = new byte[1024];

DatagramPacket dp= newDatagramPacket(arr, arr.length);//使用Socket的接收功能来接收数据

ds.receive(dp);//解析数据//获取发送过来的数据

byte[] data =dp.getData();

String shuju= new String(data,0,dp.getLength());//获取发送方的ip地址

String ip =dp.getAddress().getHostAddress();//获取发送方发送数据使用的端口

int port =dp.getPort();

System.out.println(ip+":"+port+":"+shuju);

}

}

View Code

2:使用udp实现可以一直接收数据

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetAddress;public classSend2

{public static void main(String[] args) throwsIOException

{

DatagramSocket ds= newDatagramSocket();

BufferedReader br= new BufferedReader(newInputStreamReader(System.in));

String line= null;while ((line = br.readLine()) != null)

{

DatagramPacket dp= newDatagramPacket(line.getBytes(),

line.length(), InetAddress.getByName("10.2.156.26"), 10001);

ds.send(dp);if ("over".equals(line))

{

System.out.println("程序结束!");break;

}

}

ds.close();

br.close();

}

}

Send

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.SocketException;public classReceive2 {/*** 使用udp实现可以一直接收数据

*@throwsIOException*/

public static void main(String[] args) throwsIOException {

System.out.println("接收方启动.....");//创建Socket端点,同时监听一个端口

DatagramSocket ds = new DatagramSocket(33333);while(true)

{byte[] b = new byte[1024];

DatagramPacket dp= newDatagramPacket(b, b.length);

ds.receive(dp);

String data= new String(dp.getData(),0,dp.getLength());

String ip=dp.getAddress().getHostAddress();

System.out.println(ip+":"+data);

}//ds.close();

}

}

Receive

3:使用udp实现既能发送也能接收---聊天程序

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetAddress;importjava.net.SocketException;public classUdpChat {/*** 使用udp实现既能发送也能接收---聊天程序

* 发送的同时还能接收,使用多线程

* 创建一个线程负责发送

* 创建一个线程负责接收

*@throwsSocketException*/

public static void main(String[] args) throwsSocketException {

DatagramSocket sendSocket= newDatagramSocket();

DatagramSocket receiveSocket= new DatagramSocket(44444);new Thread(newSend(sendSocket)).start();new Thread(newReceive(receiveSocket)).start();

}

}//描述接收任务

class Receive implementsRunnable

{privateDatagramSocket socket;publicReceive(DatagramSocket socket)

{this.socket =socket;

}

@Overridepublic voidrun() {while(true)

{byte[] buf= new byte[1024];

DatagramPacket dp= newDatagramPacket(buf, buf.length);try{

socket.receive(dp);

String data= new String(dp.getData(),0,dp.getLength());

String ip=dp.getAddress().getHostAddress();if("baibai".equals(data))

{

System.out.println(ip+"离开了,不聊了");

}

System.out.println(ip+":"+data);

}catch(IOException e) {

e.printStackTrace();

}

}

}

}//描述发送任务

class Send implementsRunnable

{privateDatagramSocket socket;publicSend(DatagramSocket socket)

{this.socket =socket;

}

@Overridepublic voidrun() {

BufferedReader br= new BufferedReader(newInputStreamReader(System.in));

String line= null;try{while((line = br.readLine())!=null)

{byte[] b =line.getBytes();

DatagramPacket dp= new DatagramPacket(b,b.length,InetAddress.getByName("10.2.156.255"),44444);

socket.send(dp);if("baibai".equals(line))

{break;

}

}

br.close();

socket.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

Chat

三:tcp相关代码

1:使用Tcp实现数据的发送(在服务器端把字符串变成大写返回到客户端)

代码:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagetcp;//172.17.9.1

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.PrintStream;importjava.net.InetAddress;importjava.net.Socket;importjava.net.UnknownHostException;/*** 使用Tcp实现数据的发送

* 1:创建Socket端点,同时指明连接的服务器的Ip地址和端口

* 2:从 Socket流中获取输出流

* 3:使用输出流向服务器端写入数据

*

*@throwsIOException

*@throwsUnknownHostException*/

public classClient {public static void main(String[] args) throwsIOException {//创建Tcp协议的客户端,同时指明连接的服务器的Ip地址和端口//如果这条语句执行成功,不但创建了一个客户端对象,同时也和服务器端连接成功//如果连接成功,说明和服务器端建立了一条通道//这条通道就是Socket流(也就是客户端对象),Socket流中既有字节输入流,也有字节输出流

Socket s = new Socket(InetAddress.getByName("172.17.9.1"), 8888);//创建读取键盘输入的数据的字符读取流

BufferedReader br = new BufferedReader(newInputStreamReader(System.in));//创建发送小写字符串到服务器端的字符输出流

OutputStream os =s.getOutputStream();

PrintStream ps= new PrintStream(os, true);//创建接收服务器返回的大写字符串的字符读取流

InputStream is =s.getInputStream();

BufferedReader brr= new BufferedReader(newInputStreamReader(is));//读取键盘输入的字符串并发给服务器端,并接收大写字符串

while (true) {

String line= null;if ((line = br.readLine()) != null) {if ("over".equals(line)) {

System.out.println("程序结束!!");break;

}

ps.println(line);//发送小写字符串到服务器端

String str= brr.readLine();//接收服务器返回的大写字符串

System.out.println(str);

}

}

brr.close();

ps.close();

br.close();

s.close();

}

}

Client

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagetcp;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;/***使用Tcp实现数据的接收

*1:创建Socket端点,同时监听一个端口

*2:获取客户端对象,也就是获取Socket对象,也就是获取流对象,从而和客户端使用同一个流

*3:从Socket流中获取输入流

*4:使用输入流读取客户端发送的数据

*@throwsIOException*/

public classServer {public static void main(String[] args) throwsIOException {//1:创建Socket端点,同时监听一个端口

ServerSocket ss = new ServerSocket(8888);//获取客户端对象

Socket s =ss.accept();//创建接收小写字符串的字符读取流

InputStream is =s.getInputStream();

BufferedReader br= new BufferedReader(newInputStreamReader(is));//创建发送大写字符串的字符输出流

OutputStream os =s.getOutputStream();

PrintWriter pw= new PrintWriter(os, true);//循环读取客户端,再发送大写字符串到客户端

while (true) {

String line=br.readLine();if ("over".equals(line))break;elsepw.println(line.toUpperCase());

}

pw.close();

br.close();

s.close();

ss.close();

}

}

Server

2:实现文本文件的上传:上传完成时,服务器端返回"上传成功"

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagetcp;importjava.io.BufferedReader;importjava.io.FileReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.PrintWriter;importjava.net.InetAddress;importjava.net.Socket;importjava.net.UnknownHostException;/*** 实现文本文件的上传:上传完成时,服务器端返回"上传成功"

* 客户端:

* 1:读取本地的一个文件

* 2:发送到服务器端

* 3:接收服务器端返回的"上传成功"

*@throwsIOException

*@throwsUnknownHostException*/

public classClient2 {/***@paramargs

*@throwsIOException

*@throwsUnknownHostException*/

public static void main(String[] args) throwsUnknownHostException, IOException {

Socket s= new Socket(InetAddress.getByName("172.17.9.1"), 9999);//创建读取文本文件的字符读取流

BufferedReader br = new BufferedReader(newFileReader("tempFile\\Demo.java"));//创建发送到服务器端的字符输出流

OutputStream os =s.getOutputStream();

PrintWriter pw= new PrintWriter(os, true);//创建接收服务器端返回的"上传成功"的字符读取流

InputStream is =s.getInputStream();

BufferedReader brr= new BufferedReader(newInputStreamReader(is));

String line= null;//循环读取文件并写入到服务器端

while ((line = br.readLine()) != null) {

pw.println(line);

}//向服务器端发送结束标记

s.shutdownOutput();//向服务器发送结束标记==》结束的是输出//接收服务器端返回的"上传成功"

String res =brr.readLine();

System.out.println(res);

br.close();

s.close();

}

}

Client2

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagetcp;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileWriter;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;/*** 实现文本文件的上传:上传完成时,服务器端返回"上传成功"

* 服务器端:

* 1:接收客户端发送的数据

* 2:写入到一个本地文件

* 3:发送“上传成功”

*@throwsIOException*/

public classServer2 {public static void main(String[] args) throwsIOException {

ServerSocket ss= new ServerSocket(9999);//获取客户端对象

Socket s =ss.accept();//创建接收客户端发送的数据的字符读取流

InputStream is =s.getInputStream();

BufferedReader br= new BufferedReader(newInputStreamReader(is));//创建写入到一个本地文件的字符输出流

BufferedWriter bw = new BufferedWriter(new FileWriter("tempFile\\Demo_copy.java"));//创建发送“上传成功”的字符输出流

OutputStream os =s.getOutputStream();

PrintWriter pw= new PrintWriter(os,true);//循环读取客户端发送的数据,写入到本地文件

String line = null;//读取的是客户端,所以读不到 null

while((line = br.readLine())!=null)

{

bw.write(line);

bw.newLine();

bw.flush();

}

bw.close();//发送“上传成功”

pw.println("上传成功!!");//这里如果用户println,它会自动刷新,如果是write 他不会自动刷新

s.close();

ss.close();

}

}

Server2

3:实现图片的上传

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagetcp;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.InetAddress;importjava.net.Socket;importjava.net.UnknownHostException;/*** 实现图片的上传

*@throwsIOException

*@throwsUnknownHostException*/

public classclient3 {/***@paramargs

*@throwsIOException

*@throwsUnknownHostException*/

public static void main(String[] args) throwsUnknownHostException, IOException {

Socket s= new Socket(InetAddress.getByName("172.17.9.1"),6666);//读取图片的字节读取流

FileInputStream fis = new FileInputStream("img\\img.jpg");//发送到服务器端的字节输出流

OutputStream os =s.getOutputStream();//读取上传成功的字节读取流

InputStream is =s.getInputStream();//循环读取文件,并发送到服务器端

byte[] b = new byte[1024];int len = 0;while((len = fis.read(b))!=-1)//==》读取字节的时候,返回的是int即读到的个数,参数为字符数组的引用

{

os.write(b,0,len);

}//向服务器端写入结束标记

s.shutdownOutput();//读取上传成功

byte[] arr = new byte[1024];int num =is.read(arr);

System.out.println(new String(arr,0,num));

s.close();

}

}

client3

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagetcp;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.ServerSocket;importjava.net.Socket;public classServer3 {/***@paramargs

*@throwsIOException*/

public static void main(String[] args) throwsIOException {

ServerSocket ss= new ServerSocket(6666);

Socket s=ss.accept();

InputStream is=s.getInputStream();

OutputStream os=s.getOutputStream();

FileOutputStream fos= new FileOutputStream("img\\img_copy.jpg");byte[] b = new byte[1024];int num = 0;while((num = is.read(b))!=-1)

{

fos.write(b,0,num);

}

String str= "上传成功!";

os.write(str.getBytes());

os.flush();

s.close();

ss.close();

}

}

Server3

4:实现图片的同步上传

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagetcp;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.InetAddress;importjava.net.Socket;importjava.net.UnknownHostException;public classclient4 {/***@paramargs

*@throwsIOException

*@throwsUnknownHostException*/

public static void main(String[] args) throwsUnknownHostException, IOException {

Socket s= new Socket(InetAddress.getByName("172.17.9.1"),6666);

FileInputStream fis= new FileInputStream("img\\img.jpg");

OutputStream os=s.getOutputStream();

InputStream is=s.getInputStream();byte[] b = new byte[1024];int len = 0;while((len = fis.read(b))!=-1)

{

os.write(b,0,len);

}byte[] arr = new byte[1024];

s.shutdownOutput();int num =is.read(arr);

System.out.println(new String(arr,0,num));

s.close();

}

}

client4

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagePicUpload;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.Socket;public class PicUpload implementsRunnable {

Socket s;publicPicUpload(Socket s) {this.s =s;

}

@Overridepublic voidrun() {

String ip=s.getInetAddress().getHostAddress();

System.out.println(ip+ "连接到服务器..");try{//防止文件覆盖

int num = 0;

File dir= new File("E:\\Image");if (!dir.exists())

dir.mkdir();

File file= new File(dir, ip + "(" + (++num) + ")" + ".jpg");while(file.exists())

file= new File(dir, ip + "(" + (++num) + ")" + ".jpg");

OutputStream os=s.getOutputStream();

InputStream is=s.getInputStream();

FileOutputStream fos= newFileOutputStream(file);byte[] b = new byte[1024];int len = 0;while ((len = is.read(b)) != -1) {

fos.write(b,0, len);

}

String str= "上传成功!";

os.write(str.getBytes());

os.flush();

s.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

PicUpload

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagetcp;importjava.io.IOException;importjava.net.ServerSocket;importjava.net.Socket;importPicUpload.PicUpload;public classServer4 {/***@paramargs

*@throwsIOException*/

public static void main(String[] args) throwsIOException {

ServerSocket ss= new ServerSocket(6666);while(true)

{

Socket s=ss.accept();new Thread(newPicUpload(s)).start();

}

}

}

Server4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值