UDP
receive类
package udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
//创建接收端的Socket对象(DatagramSocket)
//DatagramSocket(int port )构造数据报套接字并将其绑定到本地主机上到指定端口
DatagramSocket ds= new DatagramSocket(10086);
//创建一个数据包,用于接收数据
//Datagrampacket(byte[] buf , int length) 构造一个Datagrampacket 用于接收长度为length 的数据包
byte[] bys = new byte[1024];
DatagramPacket dp =new DatagramPacket(bys,bys.length);
//调用DatagramSocket 对象的方法接收数据
ds.receive(dp);
//解析数据包,并把数据在控制台显示
byte [] datas =dp.getData();
String dataString = new String(datas,0,dp.getLength());
System.out.println("数据是:"+ dataString);
//关闭接收端
ds.close();
}
}
send类
package udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class sendDemo {
public static void main(String[] args) throws IOException {
//创建发送端Socket对象(DatagramSocket)
//DatagramSocket()构造数据包套接字并将其绑定本地主机上的任何可用端口
DatagramSocket ds = new DatagramSocket();
//创建数据,并将数据打包
//DatagramPcacket (byte[] buf ,int lengtg ,InetAddress address, int port)
byte[] bye = "hello,udp,我来了".getBytes();
int lengt = bye.length;
InetAddress address = InetAddress.getByName("");//括号内加计算机名,或ip
int port = 10086;
DatagramPacket dp = new DatagramPacket(bye, lengt, address, port);
//调用DatagramSocket对象的方法发送数据
//void send (DatagramPacket p )从此套接字发送数据报包
ds.send(dp);
//关闭发送端
//void close()关闭此数据报套接字
ds.close();
}
}
TCP
客户端
package Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s =new Socket("",10098);//""内加ip
//获取输出流,写数据
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
//接收服务器反馈
InputStream is=s.getInputStream();
byte [] bys =new byte[1024];
int len=is.read(bys);
String data =new String(bys,0,len);
System.out.println("客户端:"+data);
//释放资源
s.close();
}
}
服务端
package Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器Socket对象
ServerSocket ss =new ServerSocket(10098);
//监听客户端连接,返回一个Socket对象
Socket s =ss.accept();
//获取输入流,读数据,并把数据显示在控制台
InputStream is = s.getInputStream();
byte [] bys =new byte[1024];
int len =is.read(bys);
String data =new String(bys,0,len);
System.out.println("服务器:"+data);
//给出反馈
OutputStream os =s.getOutputStream();
os.write("数据已收到".getBytes());
//释放资源
ss.close();
}
}
通过tcp上传图片
客户端
package picture;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
System.out.println("上传图片客户端启动中...");
//创建客户端Socket对象
Socket s =new Socket("",10088);//""内加ip
//获取输出流,写数据
File picFile = new File("D:\\sa\\1.jpg");
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(picFile));
//目的:Socket输出流 发送到服务端
OutputStream os=s.getOutputStream();
byte [] bys =new byte[1024];
int len=0;
while((len = fis.read(bys)) != -1)
{
os.write(bys, 0, len);
}
//服务端反馈
//告诉服务端 图片发送完成,不要在等着读了
s.shutdownOutput();
//读取上传成功字样
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String info = in.readLine();
System.out.println(info);
//释放资源
fis.close();
s.close();
}
}
服务端
package picture;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
System.out.println("上传图片服务端启动...");
//创建服务器Socket对象
ServerSocket ss =new ServerSocket(10088);
//监听客户端连接,返回一个Socket对象
Socket s =ss.accept();
// 写图片数据到文件的输出流
File dir = new File("D:\\sa\\photo");
// 为了避免重名覆盖,通过给重名文件进行编号
String ip = s.getInetAddress().getHostAddress();
int count = 1;
File picFile = new File(dir, ip + "(" + count + ")" + ".jpg");
while (picFile.exists()) {
count++;
picFile = new File(dir, ip + "(" + count + ")" + ".jpg");
}
FileOutputStream fos = new FileOutputStream(picFile);
//获取输入流,读数据,并把数据显示在控制台
InputStream is = s.getInputStream();
byte [] bys =new byte[1024];
int len =0;
while ((len = is.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// 给客户端一个回馈信息
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out.write("上传成功!"); // 写完需要刷新一下,或者关闭流
//释放资源
out.close();
fos.close();
s.close();
}
}