//下面是linux 下的代码: #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> #include <pthread.h> #include <string.h> #include <unistd.h> struct MYSOCK { int sock ; int port ; char IP[20]; }param; void *recv_fun(void*pParam); int main() { int m_sock = socket(AF_INET, SOCK_DGRAM,0); struct sockaddr_in addr ; char buf[100]; char IP[20]; printf("please enter connect IP:"); scanf("%s",IP); //sockaddr_in addr ; memset(&addr,0,sizeof(addr)); addr.sin_addr.s_addr = inet_addr(IP); addr.sin_family = AF_INET; addr.sin_port = htons(8000); param.sock = m_sock; strcpy(param.IP,IP); param.port = 8000; //printf("sock=%d,port=%d\n",m_sock,param.port); pthread_t th; if(pthread_create(&th,NULL,recv_fun,(void*)¶m)) { perror("create error"); return (-1); } usleep(1);//注意这里的sleep的作用,很重要,它让bind 先运行 while(1) { printf("send:"); scanf("%s",buf); if(-1 == sendto(m_sock,buf,strlen(buf)+1,0,(struct sockaddr*)&addr,sizeof(struct sockaddr))) { perror("sendto error:"); //return 0; } usleep(1); } close(m_sock); return 0; } void *recv_fun(void*pParam) { struct MYSOCK * pSock = (struct MYSOCK*)pParam; struct sockaddr_in saddr,caddr ; socklen_t len; char buf[100]; len = sizeof(struct sockaddr_in); memset(&saddr,0,sizeof(saddr)); memset(&caddr,0,sizeof(caddr)); saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_family = AF_INET ; saddr.sin_port =htons( pSock->port) ; // printf("sock=%d,port=%d\n",pSock->sock,pSock->port); if(-1 == bind(pSock->sock,(struct sockaddr*)&saddr,sizeof(struct sockaddr_in))) { perror("bind error"); return 0; } while(1) { if(-1 == recvfrom(pSock->sock,buf,sizeof(buf),0,NULL,NULL)) { perror("bind error"); } printf("recv:%s\n",buf); } } ~ ~ ~ 把这段代码编译后,在两台计算之间就可以连通了
下面是windows下的代码: // IMserver.cpp : 定义控制台应用程序的入口点。 // #include <stdio.h> #include <stdlib.h> #include <Winsock2.h> #include <Windows.h> #define BUFSIZE 100 #define UDPPORT 8000 struct MYSOCK { SOCKET sock; unsigned short port ; char IP[20]; }; DWORD WINAPI SendThread( LPVOID lpParameter // thread data ); bool flag = false; int main(int argc, char* argv[]) { WORD wVersionRequested; WSADATA wsaData; int err; int addr_long = sizeof(sockaddr_in); char buf[BUFSIZE]; sockaddr_in saddr,caddr; MYSOCK param ; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { return 1; } if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { WSACleanup( ); return 1; } SOCKET m_sock = socket(AF_INET,SOCK_DGRAM,0); if(INVALID_SOCKET == m_sock) { perror("socket error"); return 0; } // printf("%d,%d\n",m_sock,UDPPORT); param.port = UDPPORT; param.sock = m_sock ; printf("this is simple winUDP communication!\n\ please enter connect IP:"); scanf("%s",param.IP); memset(&saddr,0,sizeof(saddr)); memset(&caddr,0,sizeof(caddr)); saddr.sin_addr.S_un.S_addr = INADDR_ANY; saddr.sin_family = AF_INET; saddr.sin_port = htons(UDPPORT); HANDLE h = CreateThread( NULL, // SD 0, // initial stack size SendThread, // thread function (LPVOID)¶m , // thread argument 0, // creation option NULL // thread identifier ); if(SOCKET_ERROR == bind(m_sock,(sockaddr*)&saddr,sizeof(sockaddr_in))) { perror("bind error:"); return 0; } while(1) { memset(buf,0,sizeof(buf)); if(SOCKET_ERROR == recvfrom(m_sock,buf,sizeof(buf),0,(sockaddr*)&caddr,&addr_long)) { perror("recvfrom error:"); continue; // return 0; } if(!flag) { printf("\n"); } printf("recv:%s\n",buf); flag = true ; } system("pause"); WSACleanup( ); return 0; } DWORD WINAPI SendThread( LPVOID lpParameter // thread data ) { MYSOCK *pparam ; pparam = (MYSOCK*)lpParameter ; SOCKET m_sock = pparam->sock ; unsigned short port = pparam->port ; char buf[BUFSIZE]; sockaddr_in saddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_addr.S_un.S_addr = inet_addr(pparam->IP); saddr.sin_family = AF_INET; saddr.sin_port = htons(pparam->port); while(1) { memset(buf,0,sizeof(buf)); Sleep(1); printf("send:"); flag = false ; scanf("%s",buf); if(SOCKET_ERROR == sendto(m_sock,buf,strlen(buf)+1,0,(sockaddr*)&saddr,sizeof(sockaddr))) { perror("sendto error:"); //return -1; continue; } flag =true; } return 0; }
简单但很实用的UDP的代码(linux、windows)
最新推荐文章于 2022-11-11 10:00:00 发布