(2014.4.24)server/client代码
最简单的一个例子:服务器先listen,然后客户端发起连接请求,建立连接,然后客户端向服务端发送数据,服务端接收数据并打印出来。
服务器端代码:
#include<sys/socket.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<netinet/tcp.h>
#include"string.h"
#include"stdlib.h"
#include"stdio.h"
#define SOCKET_SND_BUF_SIZE (1024*1024)
#define SOCKET_RCV_BUF_SIZE (1024*1024)
void INFO(const char *s){
printf("%s\n",s);
}
int main(){
//create socket
int fd=socket(PF_INET,SOCK_STREAM,0);
if(fd==-1){
INFO("err:create socket");
return -1;
}else{
INFO("succeed:create socket");
}
//setsockopt
int options;
options = SOCKET_SND_BUF_SIZE;
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &options, sizeof(int));
options = SOCKET_RCV_BUF_SIZE;
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &options, sizeof(int));
options = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(int));
options = fcntl(fd, F_GETFL);
//fcntl(fd, F_SETFL, options | O_NONBLOCK);
int on = 1;
int ret;
ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(on));
if(ret==-1){
INFO("err:setsocketopt");
return -1;
}else{
INFO("succeed:setsocketopt");
}
//bind
int port=8012;
sockaddr_in addr;
memset(&addr,0,sizeof addr);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=INADDR_ANY;
addr.sin_port=htons(port);
ret=bind(fd,(const sockaddr*)&addr,sizeof addr);
if(ret==-1){
INFO("err:bind");
return -1;
}else{
INFO("succeed:bind");
}
//listen
int max_conn=100;
ret=listen(fd,max_conn);
if(ret==-1){
INFO("err:listen");
return -1;
}else{
INFO("succeed:listen");
}
//accept
int connect_fd=accept(fd,NULL,NULL);
if(connect_fd==-1){
INFO("err:accept");
return -1;
}else{
INFO("succeed:accept");
}
//recv data
char buf[4096];
char recv_buf[4096];
int offset=0;
int len=-1;
while((len=recv(connect_fd,buf,sizeof(buf),0))>0){
sprintf(recv_buf+offset,"%s",buf);
offset+=len;
}
if(len<0){
INFO("err:recv data");
return -1;
}else{
printf("succeed:recv data %d bytes:%s\n",offset,recv_buf);
}
return 0;
}
客户端代码:
#include"stdio.h"
#include"stdlib.h"
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/tcp.h>
#include<fcntl.h>
#include<string.h>
#include<arpa/inet.h>
#include<unistd.h>
#define SOCKET_SND_BUF_SIZE (1024*1024)
#define SOCKET_RCV_BUF_SIZE (1024*1024)
void INFO(const char *s){
printf("%s\n",s);
}
int main(){
//create socket
int fd=socket(PF_INET,SOCK_STREAM,0);
if(fd==-1){
INFO("err:create socket");
return -1;
}else{
INFO("create socket succeed");
}
//setsockopt
/*
int options;
options = SOCKET_SND_BUF_SIZE;
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &options, sizeof(int));
options = SOCKET_RCV_BUF_SIZE;
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &options, sizeof(int));
options = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(int));
options = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, options | O_NONBLOCK);
int on = 1;
int ret = -1;
ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(on));
if(ret==-1) return -1;
*/
//connect
int port=8012;
sockaddr_in addr;
memset(&addr,0,sizeof addr);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("10.134.89.204");
addr.sin_port=htons(port);
int ret=connect(fd,(struct sockaddr *)&addr,sizeof(struct sockaddr));
if(ret==-1){
INFO("err:building connect");
return -1;
}else{
INFO("building connect succeed");
}
//send data
char buf[4096];
char s[]="hello,world";
sprintf(buf,"what i send:%s",s);
int len=send(fd,buf,strlen(buf),0);
if(len<0){
INFO("err:send data");
return -1;
}else{
INFO("send data succeed");
printf("sending data:'%s',bytes:%d\n",buf,len);
}
return 0;
}
运行效果:
(先运行服务端,然后在运行客户端)
1、运行服务端:
2.运行客户端:
3.此时服务端: