Socket网路编程


Socket的UDP通信

1.

接受端的构造函数




2.

发送端的构造函数




3. 

常用的方法



4.

UDP完成数据的发送
/*
 * 1,创建DatagramSocket对象
 * 2,创建DatagramPacket对象,并封装数据
 * 3,发送数据
 * 4,释放流资源
 */
public class UDPSend {
public static void main(String[] args) throws IOException {
//1,创建DatagramSocket对象
DatagramSocket sendSocket = new DatagramSocket();
//2,创建DatagramPacket对象,并封装数据
//public DatagramPacket(byte[] buf, int length, InetAddress address,  int port)
//构造数据报包,用来将长度为 length 的包发送到指定主机上的指定端口号。
byte[] buffer = "hello,UDP".getBytes();
DatagramPacket dp = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("192.168.75.58"), 12306);
//3,发送数据
//public void send(DatagramPacket p) 从此套接字发送数据报包
sendSocket.send(dp);
//4,释放流资源
sendSocket.close();
}
}


5.

UDP完成数据的接收

/*
 * 1,创建DatagramSocket对象
 * 2,创建DatagramPacket对象
 * 3,接收数据存储到DatagramPacket对象中
 * 4,获取DatagramPacket对象的内容
 * 5,释放流资源
 */
public class UDPReceive {
public static void main(String[] args) throws IOException {
//1,创建DatagramSocket对象,并指定端口号
DatagramSocket receiveSocket = new DatagramSocket(12306);
//2,创建DatagramPacket对象, 创建一个空的仓库
byte[] buffer = new byte[1024];
DatagramPacket dp = new DatagramPacket(buffer, 1024);
//3,接收数据存储到DatagramPacket对象中
receiveSocket.receive(dp);
//4,获取DatagramPacket对象的内容
//谁发来的数据  getAddress()
InetAddress ipAddress = dp.getAddress();
String ip = ipAddress.getHostAddress();//获取到了IP地址
//发来了什么数据  getData()
byte[] data = dp.getData();
//发来了多少数据 getLenth()
int length = dp.getLength();
//显示收到的数据
String dataStr = new String(data,0,length);
System.out.println("IP地址:"+ip+ "数据是"+ dataStr);
//5,释放流资源
receiveSocket.close();
}

}



Socket的TCP通信

    ServerSocket是服务端的

    Socket是客户端的


1.

/*

 * 文件上传  服务器端

 */
public class TCPServer {
public static void main(String[] args) throws IOException {
//1,创建服务器,等待客户端连接
ServerSocket serverSocket = new ServerSocket(8888);
Socket clientSocket = serverSocket.accept();
//显示哪个客户端Socket连接上了服务器
InetAddress ipObject = clientSocket.getInetAddress();//得到IP地址对象
String ip = ipObject.getHostAddress(); //得到IP地址字符串
System.out.println("小样,抓到你了,连接我!!" + "IP:" + ip);
//7,获取Socket的输入流
InputStream in = clientSocket.getInputStream();
//8,创建目的地的字节输出流   D:\\upload\\192.168.74.58(1).jpg
BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream("D:\\upload\\192.168.74.58(1).jpg"));
//9,把Socket输入流中的数据,写入目的地的字节输出流中
byte[] buffer = new byte[1024];
int len = -1;
while((len = in.read(buffer)) != -1){
//写入目的地的字节输出流中
fileOut.write(buffer, 0, len);
}
//-----------------反馈信息---------------------
//10,获取Socket的输出流, 作用:写反馈信息给客户端
OutputStream out = clientSocket.getOutputStream();
//11,写反馈信息给客户端
out.write("图片上传成功".getBytes());
out.close();
fileOut.close();
in.close();
clientSocket.close();
//serverSocket.close();
}
}


2.

/*

 * 文件上传 客户端

 * 
 * public void shutdownOutput()  禁用此Socket的输出流,间接的相当于告知了服务器数据写入完毕
 */
public class TCPClient {
public static void main(String[] args) throws IOException {
//2,创建客户端Socket,连接服务器
Socket socket = new Socket("192.168.74.58", 8888);
//3,获取Socket流中的输出流,功能:用来把数据写到服务器
OutputStream out = socket.getOutputStream();
//4,创建字节输入流,功能:用来读取数据源(图片)的字节
BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream("D:\\NoDir\\test.jpg"));
//5,把图片数据写到Socket的输出流中(把数据传给服务器)
byte[] buffer = new byte[1024];
int len = -1;
while ((len = fileIn.read(buffer)) != -1){
//把数据写到Socket的输出流中
out.write(buffer, 0, len);
}
//6,客户端发送数据完毕,结束Socket输出流的写入操作,告知服务器端
socket.shutdownOutput();
//-----------------反馈信息---------------------
//12,获取Socket的输入流  作用: 读反馈信息
InputStream in = socket.getInputStream();
//13,读反馈信息
byte[] info = new byte[1024];
//把反馈信息存储到info数组中,并记录字节个数
int length = in.read(info);
//显示反馈结果
System.out.println( new String(info, 0, length) );
//关闭流
in.close();
fileIn.close();
out.close();
socket.close();
}
}


3.

/*

 * 文件上传多线程版本, 服务器端

 */
public class TCPServer {
public static void main(String[] args) throws IOException {
//1,创建服务器,等待客户端连接
ServerSocket serverSocket = new ServerSocket(6666);
//实现多个客户端连接服务器的操作
while(true){
final Socket clientSocket = serverSocket.accept();
//启动线程,完成与当前客户端的数据交互过程
new Thread(){
public void run() {
try{
//显示哪个客户端Socket连接上了服务器
InetAddress ipObject = clientSocket.getInetAddress();//得到IP地址对象
String ip = ipObject.getHostAddress(); //得到IP地址字符串
System.out.println("小样,抓到你了,连接我!!" + "IP:" + ip);
//7,获取Socket的输入流
InputStream in = clientSocket.getInputStream();
//8,创建目的地的字节输出流   D:\\upload\\192.168.74.58(1).jpg
BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream("D:\\upload\\"+ip+"("+System.currentTimeMillis()+").jpg"));
//9,把Socket输入流中的数据,写入目的地的字节输出流中
byte[] buffer = new byte[1024];
int len = -1;
while((len = in.read(buffer)) != -1){
//写入目的地的字节输出流中
fileOut.write(buffer, 0, len);
}
//-----------------反馈信息---------------------
//10,获取Socket的输出流, 作用:写反馈信息给客户端
OutputStream out = clientSocket.getOutputStream();
//11,写反馈信息给客户端
out.write("图片上传成功".getBytes());
out.close();
fileOut.close();
in.close();
clientSocket.close();
} catch(IOException e){
e.printStackTrace();
}
};
}.start();
}
//serverSocket.close();
}
}


猜你喜欢

转载自blog.csdn.net/potatoandpotato/article/details/80274900