- 论坛徽章:
- 0
|
本帖最后由 okocha-jay 于 2010-03-17 17:33 编辑
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <netdb.h>
- int main(int argc, char *argv[]) {
- int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- struct sockaddr_in serv;
- bzero(&serv, sizeof(serv));
- serv.sin_family = AF_INET;
- serv.sin_addr.s_addr = htonl(INADDR_ANY);
- serv.sin_port = htons(8888);
- if( bind(sock, (struct sockaddr *)&serv, sizeof(serv) ) )
- {
- perror("bind failure:");
- exit(0);
- }
-
- int size = 18;
- socklen_t len = sizeof(int);
- char recvbuf[32] = {0};
-
- #if 1
- if ( setsockopt(sock,SOL_SOCKET, SO_RCVBUF, &size, len))
- perror("error\n");
- printf("recvs= %.2f\n", size*1.0);
- if ( setsockopt(sock,SOL_SOCKET, SO_SNDBUF, &size, len))
- perror("error\n");
- printf("send = %.2f\n", size*1.0);
- if ( getsockopt(sock,SOL_SOCKET, SO_RCVLOWAT, &size, &len))
- perror("error\n");
- printf("low recv = %.2f\n", size*1.0);
- #endif
- struct sockaddr_in client;
- bzero(&client, sizeof(client));
- len = sizeof(client);
- int ret = recvfrom(sock, recvbuf, 32,0, (struct sockaddr *)&client, &len );
- printf("received = %d\n", ret);
- printf("from %s\n", inet_ntoa(client.sin_addr));
- int i;
- for (i=0; i< ret; ++i)
- printf("%c", recvbuf[i]);
- printf("!!!!\n");
- close(sock);
- exit(0);
- }
复制代码 |
|