linux 系统之间,网络编程,消息发送与接收
chunli@ubuntu:~/udp$ sudo apt-get update chunli@ubuntu:~/udp$ sudo apt-get install build-essential chunli@ubuntu:~/udp$ sudo apt-get install make
chunli@ubuntu:~/udp$ ll -rw-rw-r-- 1 chunli chunli 279 May 15 10:36 makefile -rw-rw-r-- 1 chunli chunli 2.3K May 15 10:36 myudp.cpp -rw-rw-r-- 1 chunli chunli 250 May 15 10:37 myudp.h -rw-rw-r-- 1 chunli chunli 259 May 15 10:37 stdafx.cpp 用这4个文件,编译出一个库文件,源代码在文下面:
chunli@ubuntu:~/udp$ make g++ -Wall -fPIC -D__UNIX -o myudp.o -c myudp.cpp g++ -o libmyudp.so myudp.o -shared ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ OK ^_^ ^_^ ^_^ ^_^ ^_^ ^_^
写一个主程序
vim main.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "myudp.h" int main(int argc, char args[]) { if (argc < 2) { bind_socket(8080); char buf[1024] = { 0 }; char IP[100] = { 0 }; memset(buf, 0, sizeof(buf)); recv_socket(buf, sizeof(buf), IP); printf("from %s : %s \n",IP,buf); } else { char buf[1024] = { "hello world!" }; send_socket("127.0.0.1", 8080, buf, strlen(buf)); } return 0; }
编译 chunli@ubuntu:~/udp$ gcc main.c -L. -lmyudp 向本机发送 chunli@ubuntu:~/udp$ ./a.out 127.0.0.1 等待接收 chunli@ubuntu:~/udp$ ./a.out from 127.0.0.1 : hello world!
源代码:
chunli@ubuntu:~/udp$ cat makefile .SUFFIXES: .cpp .o CC=g++ SRCS=myudp.cpp OBJS=$(SRCS:.cpp=.o) OUT=libmyudp.so all: $(OBJS) $(CC) -o $(OUT) $(OBJS) -shared @echo ‘^_^ ^_^ ^_^ ^_^ ^_^ ^_^ OK ^_^ ^_^ ^_^ ^_^ ^_^ ^_^‘ .cpp.o: $(CC) -Wall -fPIC -D__UNIX -o $@ -c $< clean: -rm -f $(OBJS) -rm -f core*
chunli@ubuntu:~/udp$ cat myudp.cpp // myudp.cpp : 定义 DLL 应用程序的导出函数。 // #include <string.h> #include "stdafx.h" #ifndef __UNIX #include <WinSock2.h> #pragma comment(lib, "Ws2_32.lib") #pragma warning(disable:4996) #else #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #define SOCKET int #endif SOCKET st = 0;//全局socket变量 void init_sosket() { if (st == 0) { #ifndef __UNIX unsigned short ver; WSADATA wsaData; ver = MAKEWORD(1, 1); WSAStartup(ver, &wsaData);//初始化socket char on = 1; #else int on = 1; #endif st = socket(AF_INET, SOCK_DGRAM, 0);//建立UDP socket setsockopt(st, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));//设置可以发送udp广播消息 } } void free_socket() { if (st) { #ifndef __UNIX closesocket(st); WSACleanup(); #else close(st); #endif st = 0; } } extern "C" { #ifndef __UNIX __declspec(dllexport) #endif int bind_socket(unsigned short port) { init_sosket(); struct sockaddr_in recv_addr; memset(&recv_addr, 0, sizeof(recv_addr)); recv_addr.sin_family = AF_INET; recv_addr.sin_port = htons(port);//指定port为要连接的端口号 recv_addr.sin_addr.s_addr = htonl(INADDR_ANY);//指定接收端IP地址为任意 return bind(st, (struct sockaddr *) &recv_addr, sizeof(recv_addr)); } #ifndef __UNIX __declspec(dllexport) #endif unsigned int recv_socket(char *buf, size_t bufsize, char *srcIP) { struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); #ifndef __UNIX int len = sizeof(addr); #else socklen_t len = sizeof(addr); #endif unsigned int rc = recvfrom(st, buf, bufsize, 0, (struct sockaddr *) &addr, &len);//接收到来自srcIP的消息 strcpy(srcIP, inet_ntoa(addr.sin_addr)); return rc; } #ifndef __UNIX __declspec(dllexport) #endif unsigned int send_socket(const char *destIP, unsigned short port, const char *buf, size_t bufsize) { init_sosket(); struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port);//指定port为要连接的端口号 addr.sin_addr.s_addr = inet_addr(destIP);//指定IP为要连接的IP地址 return sendto(st, buf, bufsize, 0, (struct sockaddr *) &addr, sizeof(addr));//向指定IP发送消息 } }
chunli@ubuntu:~/udp$ cat myudp.h #ifndef __MYUDP_H #define __MYUDP_H int bind_socket(unsigned short port); unsigned int recv_socket(char *buf, size_t bufsize, char *srcIP); unsigned int send_socket(const char *destIP, unsigned short port, const char *buf, size_t bufsize); #endif
chunli@ubuntu:~/udp$ cat stdafx.h // stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常更改的 // 特定于项目的包含文件 // #ifndef __UNIX #pragma once #include "targetver.h" #define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的信息 // Windows 头文件: #include <windows.h> #endif // TODO: 在此处引用程序需要的其他头文件
本文出自 “魂斗罗” 博客,请务必保留此出处http://990487026.blog.51cto.com/10133282/1773543
原文地址:http://990487026.blog.51cto.com/10133282/1773543