Android Studio接收端
package com.example.socket_udp_test;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
//import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
main_handle myhandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit);
imageView = (ImageView) findViewById(R.id.imageview);
myhandler = new main_handle();
UdpServer udpServer = new UdpServer();
udpServer.start();
}
public class main_handle extends Handler{
public main_handle(){
}
public main_handle(Looper L){
super(L);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println("gggggggggggggggggg "+msg.getData().getByteArray("data")[26000]);
int[] b = new int[msg.getData().getByteArray("data").length];
for(int i = 0; i<msg.getData().getByteArray("data").length; i++ ){
b[i] = 0xff<<24;
b[i] |= (int)(msg.getData().getByteArray("data")[i]&0xff) << 16;// << (24-8*i);
b[i] |= (int)(msg.getData().getByteArray("data")[i]&0xff) << 8;
b[i] |= (int)(msg.getData().getByteArray("data")[i]&0xff);
}
Bitmap bitmap;
bitmap = Bitmap.createBitmap(b,200,200,Bitmap.Config.ARGB_8888);
imageView.setImageBitmap(bitmap);
System.out.println("通过666666");
}
}
public class UdpServer extends Thread{
private final int PORT = 12345;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void run() {
super.run();
int DATA_LEN = 50*1024;
byte inBuff[] = new byte[DATA_LEN];
DatagramPacket inPacket;
DatagramPacket outPacket;
DatagramSocket socket;
try{
String str = "[2143213;21343fjks;213]";
socket = new DatagramSocket(PORT);
socket.setReceiveBufferSize(208*1024);
byte[] sendData = str.getBytes();
outPacket = new DatagramPacket(sendData,sendData.length,InetAddress.getByName("10.108.232.108"),8083);
socket.send(outPacket);
System.out.println("通过111111");
inPacket = new DatagramPacket(inBuff,inBuff.length);
while (true)
{
socket.receive(inPacket);
int size = socket.getReceiveBufferSize();
int sendsize = socket.getSendBufferSize();
System.out.println("通过2222222,recBufferSize="+size+" sendBufferSize="+sendsize);
// socket.close();
System.out.println("通过333333");
//获取字节数组实际的使用长度
int i = 0;
for (; i < inBuff.length; i++) {
if (inBuff[i] == '\0')
break;
}
System.out.println("字节数组的实际使用长度为:"+i);
String result = new String(inPacket.getData(),inPacket.getOffset(),inPacket.getLength());
// result = String.valueOf((int)(inPacket.getData()[0] & 0xff));
System.out.println("the number of reveived Socket is :"+"udpData:"+result);
Bundle bundle = new Bundle();
bundle.putByteArray("data",inBuff);
System.out.println("dddddddddddddddddd "+bundle.getByteArray("data")[26000]);
Message message = new Message();
message.setData(bundle);
System.out.println("fffffffffffffffff "+message.getData().getByteArray("data")[26000]);
myhandler.sendMessage(message);
System.out.println("通过4444444");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
linux系统下的发送端:
/*
* udp_client.cpp
*
* Created on: 2016年12月5日
* Author: hjj
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
using namespace std;
using namespace cv;
#define PORT 50873
int main()
{
//第 1 步 创建一个体套接字
int sockfd=socket(AF_INET,SOCK_DGRAM,0);//AF_INET用于网络通信,AF_UNIX用于单一Unix系统中进程间通信,SOCK_STREAM:流式,面向连接的比特流,顺序、可靠、双向,用于TCP通信;SOCK_DGRAM:数据报式,无连接的,定长、不可靠,用于UDP通信
int nSendBuf = 150*1024;
setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,(const char*)&nSendBuf,sizeof(int));
//第 2 步 设置 addr 结构体
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;//使用 Internet 协议
svraddr.sin_port=htons(PORT);
inet_aton("10.108.232.108",&svraddr.sin_addr);//inet_ation()函数将在cp中存储的点分十进制字符串类型的IP地址,转换为二进制的IP地址,转换后的值保存在指针inp指向的结构struct in_addr中
//第 3 步 连接服务器
connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
//将图片的字符数组赋给buff字符数组
Mat img = imread("/home/hjj/Documents/1234.jpg",0);
cout<<"img.channels()="<<img.channels()<<endl;
cout<<"img.depth()="<<img.depth()<<endl;
cout<<"img.type()="<<img.type()<<endl;
cout<<"img.elemSize()="<<img.elemSize()<<endl;
cout<<"img.elemSize1()="<<img.elemSize1()<<endl;
cout<<"img.size()="<<img.size()<<endl;
cout<<img.isContinuous()<<endl;
cout<<"img.data="<<(int)((img.data)[39999] )<< "," <<(int)( (img.data)[40000] )<< "," <<(int)( (img.data)[40001])<<endl ;
int a = 3*img.rows*img.cols;
unsigned char *array;
if(img.isContinuous()){
array = img.data;
}
cout<<"sockfd="<<sockfd<<endl;
cout<<"(int)array[26000]="<<(int)array[26000]<<endl;
//用UDP协议发送时,用sendto函数最大能发送数据的长度为:65535-IP(20)-UDP头(8)=65507字节。如果超过该值,则返回错误(-1);
int num = sendto(sockfd,array,40000,0,(struct sockaddr*)&svraddr,sizeof(svraddr)); //第 4 步 向套接字中写入字符串
cout<<num<<endl;
}
最终实现功能:发送端将一张图像数据发送至接收端,接收端通过解析在安卓虚拟机端呈现出了图像。