参考文章 http://blog.csdn.net/user_920/article/details/7671308
/*
* socket_server.c
*
* Created on: Aug 29, 2013
* Author: qiang
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <netinet/in.h>
/* Implement non-block socket server using select() */
int main(int argc, char* argv[]) {
struct sockaddr_in servaddr;
int sockfd = 0;
unsigned char buf[1000];
int size, i, max_fd;
fd_set sock_fds;
socklen_t addrlen;
struct sockaddr_in from;
struct timeval tout;
printf("Socket server start.. \n");
/* 1) Create socket using socket() system call */
sockfd = socket(AF_INET, SOCK_DGRAM,0);
/* 2) Process server's own address struct: a) Address family b) Server address,
* INADDR_ANY will automatically generate it c) Server port for client & server
* communication use */
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(7777);
/* 3) Bind the address with this socket, using bind() system call */
bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
while(1) {
tout.tv_sec = 1;
tout.tv_usec = 0;
max_fd = 0;
FD_ZERO(&sock_fds);
FD_SET(sockfd, &sock_fds);
max_fd = sockfd;
/* 4) Using select() system call to check whether socket is readable */
if(select(max_fd + 1, &sock_fds, NULL, NULL, &tout) > 0) {
if(sockfd > 0 && FD_ISSET(sockfd, &sock_fds)) {
addrlen = sizeof(from);
/* 5) Read the socket using recvfrom() system call */
size = recvfrom(sockfd, buf, 15, 0, (struct sockaddr*)&from, &addrlen);
char from_ip[100];
inet_ntop(AF_INET, &from.sin_addr.s_addr, from_ip, sizeof(from_ip));
printf("size is %d, from is %s \n", size, from_ip);
if(size < 0)
exit(1);
printf("Receive from client: %s \n", buf);
}
}
}
}
/*
* socket_client.c
*
* Created on: Aug 29, 2013
* Author: qiang
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <netinet/in.h>
int main(int argc, char* argv[]) {
int sockfd = 0, n = 0;
char sendBuff[] = "Hello Server !";
struct sockaddr_in serv_addr;
if(argc != 2) {
printf("Usage: ./socket_client ip_address \n");
return 1;
}
//memset(&sendBuff, 0, sizeof(sendBuff));
memset(&serv_addr, 0, sizeof(serv_addr));
/* 1) Create socket using socket() system calll */
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf(" Error: Couldn't create socket \n");
return 1;
}
/* 2) Process peer end address: a) Address family b) Server IP c) Server port: Port value is specified by Server end. */
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(7777);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) <= 0) {
printf("inet_pton error ! \n");
return 1;
}
/* 3) Connect to the server using connect() system call */
if(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
printf("Error: Connect failed ! \n");
return 1;
}
/* 4) Write what you want to send to server directly to socket fd using write() system call */
n = write(sockfd, sendBuff, sizeof(sendBuff));
/*{
char tmpBuf[100];
read(sockfd, tmpBuf, sizeof(sendBuff));
printf("tmpBuf is %s \n", tmpBuf);
}*/
return 0;
}
Server end:
$ gcc -g -o socket_server socket_server.c
$ gdb socket_server
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/***/eclipse_workspace/socket_server/src/socket_server...done.
(gdb) r
Starting program: /home/***/eclipse_workspace/socket_server/src/socket_server
Socket server start..
size is 15, from is xxx.xxx.xxx.xxx
Receive from client: Hello Server !
Client end:
$ gcc -g -o socket_client socket_client.c
$ gdb socket_client
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/***/next-home/eclipse_workspace/socket_client/src/socket_client...done.
(gdb) r xxx.xxx.xxx.xxx
Starting program: /home/***/next-home/eclipse_workspace/socket_client/src/socket_client 128.224.162.198
[Inferior 1 (process 19796) exited normally]
(gdb)