实训23 2018.4.27
Posted goxxiv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实训23 2018.4.27相关的知识,希望对你有一定的参考价值。
//UDPReceive.java
import java.net.*;
public class UDPReceive{
public static void main(String [] args)throws Exception{
//1.创建数据包传输对象DatagramSocket
DatagramSocket ds=new DatagramSocket(8888);
//2.创建字节数组,接收传来的对象
byte[] b=new byte[1024];
//3.创建数据包对象
DatagramPacket dp=new DatagramPacket(b,b.length);
//4.调用receive方法接收数据包
ds.receive(dp);
//5.拆包
//(1)获取ip地址
String ip=dp.getAddress().getHostAddress();
//(2)获取端口号
int port =dp.getPort();
//(3)获取数据长度
int length=dp.getLength();
System.out.println(ip+":"+port+" "+new String(b,0,length));
//6.关闭资源
ds.close();
}
}
//UDPSend.java
import java.net.*;
import java.util.Scanner;
public class UDPSend{
public static void main(String []args)throws Exception{
InetAddress inet=InetAddress.getByName("127.0.0.1");
DatagramSocket ds=new DatagramSocket();
String mes=new Scanner(System.in).next(Line();
byte[] b =mes.getBytes();
DatagramPacket dp=new DatagramPacket(b,b.length,inet,8888);
ds.send(dp);
ds.close();
}
}
//TCPClient.java
import java.io.*;
import java.net.*;
public class TCPClient{
public static void main(String [] args)throws Exception{
//1.创建Socket对象,连接服务器
Socket socket=new Socket("127.0.0.1",8888);
//2.通过客户端的套接字对象,调用获取输出流方法,将数据写向服务器
OutputStream out=socket.getOutputStream();
out.write("server".getBytes());
//接收服务器端回复
InputStream in =socket.getInputStream();
byte[] b=new byte[1024];
int len=in.read(b);
System.out.print(socket.getInetAddress+":"+socket.getPort()+" "+new String(b,0,len));
//3.关闭资源
socket.close();
}
}
//TCPServer.java
public class TCPServer{
public static void main(String [] args)throws Exception{
//1.创建服务器对象
ServerSocket socketServer=new ServerSocket(8888);
//2.调用accept方法接收客户端socket
Socket socket=secketServer.accept();
//3.获取输入流
InputStream in =socket.getInputStream();
//4.读取信息
byte[] b=new byte[1024];
int len =in.read(b);
System.out.println(socket.getInetAddress()+":"+socket.getPort()+" "+new String(b,0,len));
//回复信息
OutputStream os=socket.getOutputStream();
os.writer("received".getBytes());
//5.关闭资源
in.close;
socket.close();
serverSocket.close();
}
}
//TCPPictureClient.java
import java.io.*;
import java.net.*;
public class TCPPictureClient(){
public static void main(String [] args)throws Exception{
Socket socket=new Socket("127.0.0.1",8888);
FileInputStream fis=new FileInputStream("F:\\0.jpg");
int i=0;
byte[] b=new byte[1024];
OutputStream out=socket.getOutputStream();
while((i=fis.read(b))!=-1){
out.write(b,0,i);
}
socket.shutdownOutput();
InputStream in=socket.getInputStream();
byte[] bs=new byte[1024];
int len=in.read(bs);
System.out.println(socket.getInetAddress() + ":" + socket.getPort() );
socket.close();
fis.close();
in.close();
}
}
//TCPThread.java
import java.io.*;
import java.net.*;
public class TCPThread{
public static void main(String [] args)throws Exception{
while(true){
ServerSocket =new ServerSocket(8888);
new Thread(new Upload(socket)).start();
}
}
}
class Upload implements Runnable{
private Socket socket;
public Upload(Socket socket){
this.socket=socket;
}
public void run(){
//这里需要处理异常,但是我没有
try{
InputStream in=socket.getInputStream();
FileOutputStream fos=new FileOutputStream("G:\\a"+System.currentTimeMillis()+".jpg");
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
fos.write(b, 0, len);
}
System.out.println(socket.getInetAddress() + ":" + socket.getPort());
OutputStream os = socket.getOutputStream();
os.write("received".getBytes());
in.close();
fos.close();
socket.close();
}catch(Exception e){
throw new RuntimeException();
}
}
}
以上是关于实训23 2018.4.27的主要内容,如果未能解决你的问题,请参考以下文章