TCP socket, select model in linux.
// tcpsrv.h// wenxy created on 2005/04/12,AM// All copyright reserved.#ifndef _ALL_H#define _ALL_H// ANSC C/C++#include #include #include #include #include // linux#include #include #include #include
·
// tcpsrv.h
// wenxy created on 2005/04/12,AM
// All copyright reserved.
#ifndef _ALL_H
#define _ALL_H
// ANSC C/C++
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <assert.h>
#include <errno.h>
// linux
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/times.h> // time
#include <sys/select.h>
#include <sys/wait.h>
#include <sys/stat.h>
// micro
//#define OutErrStr(info) (printf("Error : %s/n", info))
//#define OutErrInt(info) (printf("Error : %d/n", info))
#define BUFF_SIZE 1024 * 1
#endif
// tcpsrv.c
#include "all.h"
static char chBuff[BUFF_SIZE] = {0};
// function, start TCP server
static bool startServer(int argc, char *argv[])
{
printf("Run .../n");
while (argc < 2)
{
printf("Warning: input IP and port, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
printf("IP = %s TCP port = %s/n", argv[1], argv[2]);
int nSocket = 0, nNewSocket = 0;
if((nSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Error: create socket error, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
struct sockaddr_in ServerAddr, ClientAddr;
memset(&ServerAddr, 0, sizeof(struct sockaddr_in));
memset(&ClientAddr, 0, sizeof(struct sockaddr_in));
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = ( inet_addr(argv[1]) );
ServerAddr.sin_port = (unsigned int) htons( atoi(argv[2]) );
//ServerAddr.sin_zero = 0;
if (-1 == bind(nSocket, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in)))
{
printf("Error: socket bind error, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
if (-1 == listen(nSocket, 5))
{
printf("Error: socket listen error, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
// ------------------------------------------------
// 把socket 设成非阻塞模式
int fd_flags = 0;
if ( -1 == (fd_flags = fcntl(nSocket, F_GETFL, 0) ) )
{
printf(" %s:%d fcntl F_GETFL failure! %s/n", __FILE__,/
__LINE__, strerror(errno) ) ;
printf("-----------------------------------/n/n");
}
else
{
printf( "设置连接socket为非阻塞模式!/n" );
fd_flags |= O_NONBLOCK;
if ( -1 == fcntl( nSocket, F_SETFL, fd_flags ) )
{
printf(" %s:%d fcntl F_SETFL failure! %s/n", __FILE__,/
__LINE__, strerror( errno ) );
}
}
// ------------------------------------------------
// *************************************************
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100;
fd_set fdRead;
fd_set fdWrite;
FD_ZERO(&fdRead);
FD_ZERO(&fdWrite);
printf("开始接受连接/n");
while (1)
{
FD_SET(nSocket, &fdRead);
FD_SET(nNewSocket, &fdWrite);
int nMax = 0;
if (nSocket > nNewSocket)
{
nMax = nSocket;
}
else
{
nMax = nNewSocket;
}
switch (select(nMax + 1, &fdRead, &fdWrite, NULL, &timeout))
{
case -1 : // error
printf("call select() function failure/n");
break;
case 0 : // time out
break;
default :
if (FD_ISSET(nSocket, &fdRead))
{
// 接受一个连接
unsigned int uLen = sizeof(struct sockaddr_in);
if (-1 == (nNewSocket = accept(nSocket, (struct sockaddr *)&ClientAddr, &uLen)))
{
//printf("Warning: accept connecting failed, continue ....../n");
//printf("-----------------------------------/n/n");
//continue;
//return false;
}
else
{
printf("Accept one connecting from IP [port] : %s [%d]/n",/
inet_ntoa(ClientAddr.sin_addr), ntohs(ClientAddr.sin_port));
}
}
if (FD_ISSET(nNewSocket, &fdWrite))
{
// 在新的socket上接收数据
if ( -1 == recv(nNewSocket, chBuff, sizeof(chBuff), 0) )
{
//printf("Warning: NewSocket riceive data failed. [errno = %d]/n", errno);
//printf("-----------------------------------/n/n");
}
else if (strlen(chBuff) > 0)
{
printf("Riceive data is [%d bytes]: %s/n", strlen(chBuff), chBuff);
memset(chBuff, 0, sizeof(chBuff));
//usleep(1000);
}
}
}
}
// *************************************************
if (nNewSocket)
{
close(nNewSocket);
}
if (nSocket)
{
close(nSocket);
}
return true;
}
/*
* Input: IP address, port
*/
int main(int argc, char *argv[])
{
if (startServer(argc, argv))
{
printf("Start TCP server OK/n");
}
return 0;
}
# tcpsv makefile
objets = main.o
rubbish = $(objets) tcp_server
tcp_server : main.o
g++ -g -o tcp_server main.o
main.o : main.c all.h
g++ -g -c main.c
.PHONY : clean
clean :
-rm $(rubbish)
# end makefile
// tcp client all.h
// wenxy created on 2005/04/12,AM
// All copyright reserved.
#ifndef _ALL_H
#define _ALL_H
// ANSC C/C++
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <assert.h>
// linux
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/stat.h>
// micro
//#define OutErrStr(info) (printf("Error : %s/n", info))
//#define OutErrInt(info) (printf("Error : %d/n", info))
#define BUFF_SIZE 1024 * 1
#endif
// main.c, tcp client
#include "all.h"
//
char chBuff[BUFF_SIZE] = "This is clinet init data,/n这是初始化数据。/n";
// function
// connecting the TCP server
static bool StartClient(int argc, char *argv[])
{
while (argc < 2)
{
printf("Warning: input IP and port, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
printf("IP = %s TCP port = %s/n", argv[1], argv[2]);
unsigned int nSocket = 0;
if( (-1 == (nSocket = socket(AF_INET, SOCK_STREAM, 0))) )
{
printf("Error: create socket error, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
struct sockaddr_in ServerAddr;
memset(&ServerAddr, 0, sizeof(struct sockaddr_in));
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = ( inet_addr(argv[1]) );
ServerAddr.sin_port = htons( atoi(argv[2]) );
unsigned int uLen = sizeof(struct sockaddr_in);
if ( -1 == (connect(nSocket, (struct sockaddr *) &ServerAddr, uLen)) )
{
printf("Error: connect TCP server failed, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
// receive and send data
int uData = 0;
unsigned int nDataLen = 0;
while (1)
{
if ( -1 == (nDataLen = send(nSocket, chBuff, strlen(chBuff), 0)) )
{
printf("Error: send data failed./n");
printf("-----------------------------------/n/n");
continue;
}
else
{
printf("Send data OK data is : %s [%d bytes]/n", chBuff, nDataLen);
memset(chBuff, 0, sizeof(chBuff));
uData ++;
//strcpy(chBuff, "wenxy");
sprintf(chBuff, "%d", uData);
//memcpy(chBuff, &(char)uData, sizeof(uData));
}
sleep(2);
}
if (nSocket)
{
close(nSocket);
}
return true;
}
/*
* Input: IP address, port
*/
int main(int argc, char *argv[])
{
printf("Run .../n");
if (StartClient(argc, argv))
{
printf("connect TCP server OK/n");
}
return 0;
}
# makefile, tcp client
objets = main.o
rubbish = $(objets) tcp_clinet
tcp_clinet : main.o
g++ -o tcp_clinet main.o
main.o : main.c all.h
g++ -c main.c
.PHONY : clean
clean :
-rm $(rubbish)
# end makefile
// wenxy created on 2005/04/12,AM
// All copyright reserved.
#ifndef _ALL_H
#define _ALL_H
// ANSC C/C++
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <assert.h>
#include <errno.h>
// linux
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/times.h> // time
#include <sys/select.h>
#include <sys/wait.h>
#include <sys/stat.h>
// micro
//#define OutErrStr(info) (printf("Error : %s/n", info))
//#define OutErrInt(info) (printf("Error : %d/n", info))
#define BUFF_SIZE 1024 * 1
#endif
// tcpsrv.c
#include "all.h"
static char chBuff[BUFF_SIZE] = {0};
// function, start TCP server
static bool startServer(int argc, char *argv[])
{
printf("Run .../n");
while (argc < 2)
{
printf("Warning: input IP and port, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
printf("IP = %s TCP port = %s/n", argv[1], argv[2]);
int nSocket = 0, nNewSocket = 0;
if((nSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Error: create socket error, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
struct sockaddr_in ServerAddr, ClientAddr;
memset(&ServerAddr, 0, sizeof(struct sockaddr_in));
memset(&ClientAddr, 0, sizeof(struct sockaddr_in));
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = ( inet_addr(argv[1]) );
ServerAddr.sin_port = (unsigned int) htons( atoi(argv[2]) );
//ServerAddr.sin_zero = 0;
if (-1 == bind(nSocket, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in)))
{
printf("Error: socket bind error, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
if (-1 == listen(nSocket, 5))
{
printf("Error: socket listen error, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
// ------------------------------------------------
// 把socket 设成非阻塞模式
int fd_flags = 0;
if ( -1 == (fd_flags = fcntl(nSocket, F_GETFL, 0) ) )
{
printf(" %s:%d fcntl F_GETFL failure! %s/n", __FILE__,/
__LINE__, strerror(errno) ) ;
printf("-----------------------------------/n/n");
}
else
{
printf( "设置连接socket为非阻塞模式!/n" );
fd_flags |= O_NONBLOCK;
if ( -1 == fcntl( nSocket, F_SETFL, fd_flags ) )
{
printf(" %s:%d fcntl F_SETFL failure! %s/n", __FILE__,/
__LINE__, strerror( errno ) );
}
}
// ------------------------------------------------
// *************************************************
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100;
fd_set fdRead;
fd_set fdWrite;
FD_ZERO(&fdRead);
FD_ZERO(&fdWrite);
printf("开始接受连接/n");
while (1)
{
FD_SET(nSocket, &fdRead);
FD_SET(nNewSocket, &fdWrite);
int nMax = 0;
if (nSocket > nNewSocket)
{
nMax = nSocket;
}
else
{
nMax = nNewSocket;
}
switch (select(nMax + 1, &fdRead, &fdWrite, NULL, &timeout))
{
case -1 : // error
printf("call select() function failure/n");
break;
case 0 : // time out
break;
default :
if (FD_ISSET(nSocket, &fdRead))
{
// 接受一个连接
unsigned int uLen = sizeof(struct sockaddr_in);
if (-1 == (nNewSocket = accept(nSocket, (struct sockaddr *)&ClientAddr, &uLen)))
{
//printf("Warning: accept connecting failed, continue ....../n");
//printf("-----------------------------------/n/n");
//continue;
//return false;
}
else
{
printf("Accept one connecting from IP [port] : %s [%d]/n",/
inet_ntoa(ClientAddr.sin_addr), ntohs(ClientAddr.sin_port));
}
}
if (FD_ISSET(nNewSocket, &fdWrite))
{
// 在新的socket上接收数据
if ( -1 == recv(nNewSocket, chBuff, sizeof(chBuff), 0) )
{
//printf("Warning: NewSocket riceive data failed. [errno = %d]/n", errno);
//printf("-----------------------------------/n/n");
}
else if (strlen(chBuff) > 0)
{
printf("Riceive data is [%d bytes]: %s/n", strlen(chBuff), chBuff);
memset(chBuff, 0, sizeof(chBuff));
//usleep(1000);
}
}
}
}
// *************************************************
if (nNewSocket)
{
close(nNewSocket);
}
if (nSocket)
{
close(nSocket);
}
return true;
}
/*
* Input: IP address, port
*/
int main(int argc, char *argv[])
{
if (startServer(argc, argv))
{
printf("Start TCP server OK/n");
}
return 0;
}
# tcpsv makefile
objets = main.o
rubbish = $(objets) tcp_server
tcp_server : main.o
g++ -g -o tcp_server main.o
main.o : main.c all.h
g++ -g -c main.c
.PHONY : clean
clean :
-rm $(rubbish)
# end makefile
// tcp client all.h
// wenxy created on 2005/04/12,AM
// All copyright reserved.
#ifndef _ALL_H
#define _ALL_H
// ANSC C/C++
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <assert.h>
// linux
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/stat.h>
// micro
//#define OutErrStr(info) (printf("Error : %s/n", info))
//#define OutErrInt(info) (printf("Error : %d/n", info))
#define BUFF_SIZE 1024 * 1
#endif
// main.c, tcp client
#include "all.h"
//
char chBuff[BUFF_SIZE] = "This is clinet init data,/n这是初始化数据。/n";
// function
// connecting the TCP server
static bool StartClient(int argc, char *argv[])
{
while (argc < 2)
{
printf("Warning: input IP and port, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
printf("IP = %s TCP port = %s/n", argv[1], argv[2]);
unsigned int nSocket = 0;
if( (-1 == (nSocket = socket(AF_INET, SOCK_STREAM, 0))) )
{
printf("Error: create socket error, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
struct sockaddr_in ServerAddr;
memset(&ServerAddr, 0, sizeof(struct sockaddr_in));
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = ( inet_addr(argv[1]) );
ServerAddr.sin_port = htons( atoi(argv[2]) );
unsigned int uLen = sizeof(struct sockaddr_in);
if ( -1 == (connect(nSocket, (struct sockaddr *) &ServerAddr, uLen)) )
{
printf("Error: connect TCP server failed, process exit./n");
printf("-----------------------------------/n/n");
return false;
}
// receive and send data
int uData = 0;
unsigned int nDataLen = 0;
while (1)
{
if ( -1 == (nDataLen = send(nSocket, chBuff, strlen(chBuff), 0)) )
{
printf("Error: send data failed./n");
printf("-----------------------------------/n/n");
continue;
}
else
{
printf("Send data OK data is : %s [%d bytes]/n", chBuff, nDataLen);
memset(chBuff, 0, sizeof(chBuff));
uData ++;
//strcpy(chBuff, "wenxy");
sprintf(chBuff, "%d", uData);
//memcpy(chBuff, &(char)uData, sizeof(uData));
}
sleep(2);
}
if (nSocket)
{
close(nSocket);
}
return true;
}
/*
* Input: IP address, port
*/
int main(int argc, char *argv[])
{
printf("Run .../n");
if (StartClient(argc, argv))
{
printf("connect TCP server OK/n");
}
return 0;
}
# makefile, tcp client
objets = main.o
rubbish = $(objets) tcp_clinet
tcp_clinet : main.o
g++ -o tcp_clinet main.o
main.o : main.c all.h
g++ -c main.c
.PHONY : clean
clean :
-rm $(rubbish)
# end makefile
更多推荐
已为社区贡献4条内容
所有评论(0)