分类: 系统运维
2009-08-05 21:22:18
server端:
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);//建立socket
struct sockaddr_in ServerAddr;
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = htonl( INADDR_ANY );//地址绑定为any
ServerAddr.sin_port = htons( SERVER_PORT );
bind(nSocket, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in));
listen(nsocket,num);
accept(nSocket, (struct sockaddr *)&ClientAddr, &uLen)
client端:
nSocket = socket(AF_INET, SOCK_STREAM, 0);
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]) );//服务器IP地址
ServerAddr.sin_port = htons( atoi(argv[2]) );//服务器tcp端口
connect(nSocket, (struct sockaddr *) &ServerAddr, uLen)
send(nSocket, chBuff, strlen(chBuff), 0)