UDP发送和接收
MainRecv.cpp
#include <iostream> #include <WinSock2.h> #include <sstream> #pragma comment(lib,"ws2_32.lib") #define RECV_IP "127.0.0.1" #define RECV_PORT 8899 #define LEN_RECV_BUF 2048 int main() { std::ostringstream ossTemp; WSAData wsaData; std::cout<<"Start..."<<std::endl; WSAStartup(MAKEWORD(2,2),&wsaData); SOCKET sktRecv=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); sockaddr_in addrRecv; addrRecv.sin_family=AF_INET; addrRecv.sin_addr.s_addr=inet_addr(RECV_IP); //htonl(INADDR_ANY); addrRecv.sin_port=htons(RECV_PORT); int retVal; retVal=bind(sktRecv,(sockaddr*)&addrRecv,sizeof(addrRecv)); sockaddr_in addrClient; char buf[LEN_RECV_BUF]; int len=sizeof(addrClient); while(true) { memset(buf,0,LEN_RECV_BUF); retVal=recvfrom(sktRecv,buf,LEN_RECV_BUF,0,(sockaddr*)&addrClient,&len); if(SOCKET_ERROR==retVal) { ossTemp<<"recvfrom error : "<<retVal<<"\n"; OutputDebugString(ossTemp.str().c_str()); break; } if(strlen(buf)>0) std::cout<<buf<<std::endl; } closesocket(sktRecv); WSACleanup(); std::cout<<"Recv Over"<<std::endl; getchar(); return -1; }
MainSend.cpp
#include <iostream> #include <sstream> #include <WinSock2.h> #define IP_SEND "127.0.0.1" #define PORT_SEND 8899 #define LEN_SEND_BUF 1024 #pragma comment(lib,"ws2_32.lib") int main() { WSAData wsaData; WSAStartup(MAKEWORD(2,2),&wsaData); std::cout<<"Begin Sending..."<<std::endl; std::ostringstream ossTemp; SOCKET sktSend=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); sockaddr_in addrSend; addrSend.sin_family=AF_INET; addrSend.sin_addr.s_addr=inet_addr(IP_SEND); addrSend.sin_port=htons(PORT_SEND); int retVal; char buf[LEN_SEND_BUF]; int len=sizeof(addrSend); int index=0; while(true) { if(index>255) break; ossTemp.str(""); ossTemp<<index; memset(buf,0,LEN_SEND_BUF); memcpy(buf,ossTemp.str().c_str(),sizeof(int)); retVal=sendto(sktSend,buf,LEN_SEND_BUF,0,(sockaddr*)&addrSend,len); if(SOCKET_ERROR==retVal) { ossTemp.str(""); ossTemp<<"sendto error : "<<retVal; OutputDebugString( ossTemp.str().c_str() ); break; } std::cout<<"Send : "<<buf<<std::endl; index++; } closesocket(sktSend); WSACleanup(); std::cout<<"Send Over"<<std::endl; getchar(); return 0; }
========================
linux下socket使用备忘:
先看文件列表:
Config.h...........................................
#define SERVER_IP "192.168.6.129"
#define SERVER_PORT 12345
Makefile
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> //inet_addr需要引用
#include <string.h> //memset方法需要引用
#include <unistd.h> //close(sock_id)方法需要引用
#include "Config.h"
#define BUFSIZE 1024
int main(int argc,char* argv[])
{
int server_sockfd;
int client_sockfd;
int len;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size;
char buf[BUFSIZE];
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=inet_addr("192.168.6.129");
//inet_pton(AF_INET,"192.168.6.129",&(server_addr.sin_addr));
server_addr.sin_port=htons(SERVER_PORT);
if( (server_sockfd=socket(AF_INET,SOCK_STREAM,0))<0 ){
perror("create sockets failed.");
return -1;
}
if( bind(server_sockfd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr))<0 ){
perror("bind the socket failed.");
return -1;
}
listen(server_sockfd,5);
sin_size=sizeof(struct sockaddr_in);
if( (client_sockfd=accept(server_sockfd,(struct sockaddr*)&client_addr,(socklen_t*)&sin_size))<0 ){
perror("accept the connect failed.");
return -1;
}
printf( "accept client %s\n",inet_ntoa(client_addr.sin_addr) );
len=send(client_sockfd,"Welcome to my server\n",21,0);
while( (len=recv(client_sockfd,buf,BUFSIZE,0))>0 ){
buf[len]='\0';
printf("%s\n",buf);
if( send(client_sockfd,buf,len,0)<0 ){
perror("write failed.");
return -1;
}
}
close(client_sockfd);
close(server_sockfd);
return 0;
}
MySockTcpClient.cpp...........................................
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include "Config.h"
#define BUFSIZE 1024
int main(int argc,char* argv[])
{
int client_sockfd;
int len;
struct sockaddr_in server_addr;
char buf[BUFSIZE];
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=inet_addr(SERVER_IP);
//inet_pton(AF_INET,SERVER_IP,&(server_addr.sin_addr));
server_addr.sin_port=htons(SERVER_PORT);
if( (client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0 ){
perror("create the socket failed.");
return -1;
}
if( connect(client_sockfd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr))<0 ){
perror("connect the server failed.");
}
printf("connected to server\n");
len=recv( client_sockfd,buf,BUFSIZE,0 );
buf[len]='\0';
printf("%s",buf);
while(1){
printf("Enter string to send:");
scanf("%s",buf);
if( !strcmp(buf,"quit") )
break;
len=send(client_sockfd,buf,strlen(buf),0);
len=recv(client_sockfd,buf,BUFSIZE,0);
buf[len]='\0';
printf("received:%s\n",buf);
}
close(client_sockfd);
return 0;
}
Makefile..................................................
CC=g++
CFLAGS=-I. -g
all:MySockTcpServer MySockTcpClient
MySockTcpServer:MySockTcpServer.o
$(CC) -o MySockTcpServer MySockTcpServer.o -I.
MySockTcpClient:MySockTcpClient.o
$(CC) -o MySockTcpClient MySockTcpClient.o -I.
clean:
rm -f *.o MySockTcpClient MySockTcpServer
注意:
比较window,差别是:
1,头文件不同;
2,对socket的类型定义上有差别;
设置端口缓存大小:
//2. may set socket behavior
int nErrCode;
unsigned int uiRcvBuf;
int uiRcvBufLen = sizeof(uiRcvBuf);
nErrCode= getsockopt(m_tcpClient, SOL_SOCKET, SO_RCVBUF, (char*)&uiRcvBuf, &uiRcvBufLen);
if(SOCKET_ERROR == nErrCode) return FALSE;
if(uiRcvBuf!=1024*8){
//uiRcvBuf *= BUF_MULTIPLE;
uiRcvBuf=1024*8;
nErrCode = setsockopt(m_tcpClient, SOL_SOCKET, SO_RCVBUF,(char*)&uiRcvBuf, uiRcvBufLen);
if (SOCKET_ERROR == nErrCode) return FALSE;
nErrCode=getsockopt(m_tcpClient, SOL_SOCKET, SO_RCVBUF,(char*)&uiRcvBuf, &uiRcvBufLen);
if (SOCKET_ERROR == nErrCode) return FALSE;
if(uiRcvBuf!=1024*8) return FALSE;
}
#include <iostream> #include <WinSock2.h> #include <sstream> #pragma comment(lib,"ws2_32.lib") #define RECV_IP "127.0.0.1" #define RECV_PORT 8899 #define LEN_RECV_BUF 2048 int main() { std::ostringstream ossTemp; WSAData wsaData; std::cout<<"Start..."<<std::endl; WSAStartup(MAKEWORD(2,2),&wsaData); SOCKET sktRecv=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); sockaddr_in addrRecv; addrRecv.sin_family=AF_INET; addrRecv.sin_addr.s_addr=inet_addr(RECV_IP); //htonl(INADDR_ANY); addrRecv.sin_port=htons(RECV_PORT); int retVal; retVal=bind(sktRecv,(sockaddr*)&addrRecv,sizeof(addrRecv)); sockaddr_in addrClient; char buf[LEN_RECV_BUF]; int len=sizeof(addrClient); while(true) { memset(buf,0,LEN_RECV_BUF); retVal=recvfrom(sktRecv,buf,LEN_RECV_BUF,0,(sockaddr*)&addrClient,&len); if(SOCKET_ERROR==retVal) { ossTemp<<"recvfrom error : "<<retVal<<"\n"; OutputDebugString(ossTemp.str().c_str()); break; } if(strlen(buf)>0) std::cout<<buf<<std::endl; } closesocket(sktRecv); WSACleanup(); std::cout<<"Recv Over"<<std::endl; getchar(); return -1; }
MainSend.cpp
#include <iostream> #include <sstream> #include <WinSock2.h> #define IP_SEND "127.0.0.1" #define PORT_SEND 8899 #define LEN_SEND_BUF 1024 #pragma comment(lib,"ws2_32.lib") int main() { WSAData wsaData; WSAStartup(MAKEWORD(2,2),&wsaData); std::cout<<"Begin Sending..."<<std::endl; std::ostringstream ossTemp; SOCKET sktSend=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); sockaddr_in addrSend; addrSend.sin_family=AF_INET; addrSend.sin_addr.s_addr=inet_addr(IP_SEND); addrSend.sin_port=htons(PORT_SEND); int retVal; char buf[LEN_SEND_BUF]; int len=sizeof(addrSend); int index=0; while(true) { if(index>255) break; ossTemp.str(""); ossTemp<<index; memset(buf,0,LEN_SEND_BUF); memcpy(buf,ossTemp.str().c_str(),sizeof(int)); retVal=sendto(sktSend,buf,LEN_SEND_BUF,0,(sockaddr*)&addrSend,len); if(SOCKET_ERROR==retVal) { ossTemp.str(""); ossTemp<<"sendto error : "<<retVal; OutputDebugString( ossTemp.str().c_str() ); break; } std::cout<<"Send : "<<buf<<std::endl; index++; } closesocket(sktSend); WSACleanup(); std::cout<<"Send Over"<<std::endl; getchar(); return 0; }
========================
linux下socket使用备忘:
先看文件列表:
Config.h...........................................
#define SERVER_IP "192.168.6.129"
#define SERVER_PORT 12345
Makefile
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> //inet_addr需要引用
#include <string.h> //memset方法需要引用
#include <unistd.h> //close(sock_id)方法需要引用
#include "Config.h"
#define BUFSIZE 1024
int main(int argc,char* argv[])
{
int server_sockfd;
int client_sockfd;
int len;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size;
char buf[BUFSIZE];
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=inet_addr("192.168.6.129");
//inet_pton(AF_INET,"192.168.6.129",&(server_addr.sin_addr));
server_addr.sin_port=htons(SERVER_PORT);
if( (server_sockfd=socket(AF_INET,SOCK_STREAM,0))<0 ){
perror("create sockets failed.");
return -1;
}
if( bind(server_sockfd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr))<0 ){
perror("bind the socket failed.");
return -1;
}
listen(server_sockfd,5);
sin_size=sizeof(struct sockaddr_in);
if( (client_sockfd=accept(server_sockfd,(struct sockaddr*)&client_addr,(socklen_t*)&sin_size))<0 ){
perror("accept the connect failed.");
return -1;
}
printf( "accept client %s\n",inet_ntoa(client_addr.sin_addr) );
len=send(client_sockfd,"Welcome to my server\n",21,0);
while( (len=recv(client_sockfd,buf,BUFSIZE,0))>0 ){
buf[len]='\0';
printf("%s\n",buf);
if( send(client_sockfd,buf,len,0)<0 ){
perror("write failed.");
return -1;
}
}
close(client_sockfd);
close(server_sockfd);
return 0;
}
MySockTcpClient.cpp...........................................
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include "Config.h"
#define BUFSIZE 1024
int main(int argc,char* argv[])
{
int client_sockfd;
int len;
struct sockaddr_in server_addr;
char buf[BUFSIZE];
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=inet_addr(SERVER_IP);
//inet_pton(AF_INET,SERVER_IP,&(server_addr.sin_addr));
server_addr.sin_port=htons(SERVER_PORT);
if( (client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0 ){
perror("create the socket failed.");
return -1;
}
if( connect(client_sockfd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr))<0 ){
perror("connect the server failed.");
}
printf("connected to server\n");
len=recv( client_sockfd,buf,BUFSIZE,0 );
buf[len]='\0';
printf("%s",buf);
while(1){
printf("Enter string to send:");
scanf("%s",buf);
if( !strcmp(buf,"quit") )
break;
len=send(client_sockfd,buf,strlen(buf),0);
len=recv(client_sockfd,buf,BUFSIZE,0);
buf[len]='\0';
printf("received:%s\n",buf);
}
close(client_sockfd);
return 0;
}
Makefile..................................................
CC=g++
CFLAGS=-I. -g
all:MySockTcpServer MySockTcpClient
MySockTcpServer:MySockTcpServer.o
$(CC) -o MySockTcpServer MySockTcpServer.o -I.
MySockTcpClient:MySockTcpClient.o
$(CC) -o MySockTcpClient MySockTcpClient.o -I.
clean:
rm -f *.o MySockTcpClient MySockTcpServer
注意:
比较window,差别是:
1,头文件不同;
2,对socket的类型定义上有差别;
设置端口缓存大小:
//2. may set socket behavior
int nErrCode;
unsigned int uiRcvBuf;
int uiRcvBufLen = sizeof(uiRcvBuf);
nErrCode= getsockopt(m_tcpClient, SOL_SOCKET, SO_RCVBUF, (char*)&uiRcvBuf, &uiRcvBufLen);
if(SOCKET_ERROR == nErrCode) return FALSE;
if(uiRcvBuf!=1024*8){
//uiRcvBuf *= BUF_MULTIPLE;
uiRcvBuf=1024*8;
nErrCode = setsockopt(m_tcpClient, SOL_SOCKET, SO_RCVBUF,(char*)&uiRcvBuf, uiRcvBufLen);
if (SOCKET_ERROR == nErrCode) return FALSE;
nErrCode=getsockopt(m_tcpClient, SOL_SOCKET, SO_RCVBUF,(char*)&uiRcvBuf, &uiRcvBufLen);
if (SOCKET_ERROR == nErrCode) return FALSE;
if(uiRcvBuf!=1024*8) return FALSE;
}
来源:https://www.cnblogs.com/tangxiacun/p/4466456.html