GCC下的BSD Socket简单使用
亲手在用GCC编过没问题。之前老是写成C++的,要用G++才能编得过。
包含头文件:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netdb.h>
#include <fcntl.h>
服务器篇:
int createUdpServer()
{
int g_socketSrv;//最好弄成全局的。
struct sockaddr_in addrSrv;
memset(&addrSrv,0,sizeof(struct sockaddr_in));
addrSrv.sin_family = AF_INET;
addrSrv.sin_addr.s_addr=inet_addr("127.0.0.1");
addrSrv.sin_port = htons(20000);
if((g_socketSrv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
return -1;
//绑定socket
if (-1 == bind(g_socketSrv, (struct sockaddr*)&addrSrv, sizeof(struct sockaddr_in)))
{
close(g_socketSrv);
g_socketSrv = -1;
return -1;
}
//设置堵塞模式
int flags = fcntl(g_socketSrv, F_GETFL, 0);
fcntl(g_socketSrv, F_SETFL, flags & ~O_NONBLOCK);
//循环收数据
struct sockaddr_in addrClient;
int len = sizeof(struct sockaddr_in);
int err = -1;
while (1) {
char recvBuf[256];
memset(recvBuf, 0, sizeof(char)*256);
recvfrom(g_socketSrv, recvBuf, 256, 0, (struct sockaddr*) &addrClient, (socklen_t*)&len);
int len2 = strlen(recvBuf);
if(len2>0)
{
//收到数据。。。recvBuf
}
sleep(2);
}
}
客户端:
-void sendMsg()
{
int sock_fd;
struct sockaddr_in receiver_addr;
int port = 20000;
char palam[200];
strcpy(palam,http://www.baidu.com/);
if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
}
receiver_addr.sin_family = AF_INET;
receiver_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
receiver_addr.sin_port = htons(port);
//就发送一条信息
int ret = sendto(sock_fd, palam, strlen(palam), 0,(struct sockaddr*)&receiver_addr,sizeof(receiver_addr));
if (-1 == ret)
{
//sendto error";
}
close(sock_fd);
}
包含头文件:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netdb.h>
#include <fcntl.h>
服务器篇:
int createUdpServer()
{
int g_socketSrv;//最好弄成全局的。
struct sockaddr_in addrSrv;
memset(&addrSrv,0,sizeof(struct sockaddr_in));
addrSrv.sin_family = AF_INET;
addrSrv.sin_addr.s_addr=inet_addr("127.0.0.1");
addrSrv.sin_port = htons(20000);
if((g_socketSrv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
return -1;
//绑定socket
if (-1 == bind(g_socketSrv, (struct sockaddr*)&addrSrv, sizeof(struct sockaddr_in)))
{
close(g_socketSrv);
g_socketSrv = -1;
return -1;
}
//设置堵塞模式
int flags = fcntl(g_socketSrv, F_GETFL, 0);
fcntl(g_socketSrv, F_SETFL, flags & ~O_NONBLOCK);
//循环收数据
struct sockaddr_in addrClient;
int len = sizeof(struct sockaddr_in);
int err = -1;
while (1) {
char recvBuf[256];
memset(recvBuf, 0, sizeof(char)*256);
recvfrom(g_socketSrv, recvBuf, 256, 0, (struct sockaddr*) &addrClient, (socklen_t*)&len);
int len2 = strlen(recvBuf);
if(len2>0)
{
//收到数据。。。recvBuf
}
sleep(2);
}
}
客户端:
-void sendMsg()
{
int sock_fd;
struct sockaddr_in receiver_addr;
int port = 20000;
char palam[200];
strcpy(palam,http://www.baidu.com/);
if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
}
receiver_addr.sin_family = AF_INET;
receiver_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
receiver_addr.sin_port = htons(port);
//就发送一条信息
int ret = sendto(sock_fd, palam, strlen(palam), 0,(struct sockaddr*)&receiver_addr,sizeof(receiver_addr));
if (-1 == ret)
{
//sendto error";
}
close(sock_fd);
}
先顶