-
Notifications
You must be signed in to change notification settings - Fork 0
/
whu_SockRecv.cpp
121 lines (114 loc) · 2.54 KB
/
whu_SockRecv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "StdAfx.h"
#include "whu_SockRecv.h"
int whu_SockRecv::Initialize()
{
cout<<"socket initialize start....."<<endl;
sockaddr_in addr,addr1;
int n=sizeof(addr1);
WSAData data;
WORD wVersionRequested=MAKEWORD(2,0);
::WSAStartup(wVersionRequested,&data);
if((s=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)
{
printf("socket error");
return 0;
}
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(80);
addr.sin_addr.S_un.S_addr=INADDR_ANY;
if(bind(s,(struct sockaddr*)&addr,sizeof(addr))==SOCKET_ERROR)
{
printf("bind error \n");
return 0;
}
else printf("bind success \n");
if(listen(s,5)==SOCKET_ERROR)
{
printf("listen error\n");
return 0;
}
else printf("listen success\n");
if((s1=accept(s,(struct sockaddr*)&addr1,&n))==INVALID_SOCKET)
{
printf("accept error\n");
return 0;
}
else printf("accept success");
cout<<"socket initialize success"<<endl;
return 0;
}
int whu_SockRecv::ReceiveAngle(double *m_Angle)
{
////unsigned char m_RecvBuf[7];
//char RecvBuf[50];
////double m_Angle[6]={90};
////int angle[6]={90};
//while(recv(s1,RecvBuf,50,0)>0)
//{
// cout<<(int)RecvBuf[0]<<" ";
// memcpy(m_Angle,RecvBuf+1,48);
// //memcpy(m_RecvBuf,RecvBuf,7);
// for (int i=0;i<6;i++)
// {
// //angle[i-1]=(int)m_RecvBuf[i];
// cout<<m_Angle[i]<<" ";
// }
// cout<<endl;
// //printf("角度%s:",RecvBuf);
//}
//return 0;
//下面是接收double型舵机角度的//
char RecvBuf[50];
double m_Tangle[7];
int RecvLength=recv(s1,RecvBuf,50,0);
memcpy(m_Angle,RecvBuf+1,48);
//while(recv(s1,RecvBuf,50,0)>0)
//{
// memcpy(m_Angle,RecvBuf+1,48);
// for (int i=0;i<6;i++)
// {
// //angle[i-1]=(int)m_RecvBuf[i];
// cout<<m_Angle[i]<<" ";
// }
// cout<<endl;
//}
return 0;
}
int whu_SockRecv::ReceiveVideo(IplImage* m_img)
{
int recvbyte=0;//这个值,只是一帧图像的一行//
//char m_ImgBuf[640][480];
char recvbuf[1924];
uchar* ptr;
int line;
for(int y=0;y<m_img->height;y++)
{
recvbyte=recv(s1,recvbuf,1924,0); //
memcpy(&line,recvbuf,4);
if (line>=0&&line<m_img->height)
{
ptr=(uchar*)(m_img->imageData+y*m_img->widthStep);
memcpy(ptr,recvbuf+4,1920);
}
}
////为什么这样会死机啊!!!
//for(int y=0;y<m_img->height;y++)
//{
// recvbyte=recv(s1,recvbuf,1924,0); //
// memcpy(&line,recvbuf,4);
// if (line>=0&&line<m_img->height)
// {
// ptr=(uchar*)(m_img->imageData+y*m_img->widthStep);
// }
// memcpy(ptr,recvbuf+4,1920); //为什么这样会死机啊!!!
//}
return recvbyte;
}
int whu_SockRecv::Close()
{
::closesocket(s);
::closesocket(s1);
::WSACleanup();
return 0;
}