Linux socket编程实战(阻塞)

  • 1.服务端
#include
#include
#include
#include
#include
#include


int main(int argc, char* argv[]){

int fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0){
	printf("create socket failed!, errno=%d\n",errno);
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(8080);

int ret;
ret = bind(fd, (struct sockaddr*) &addr, sizeof(addr));
if(ret == -1){
	printf("bind failed!, errno=%d",errno);
	close(fd);
}
ret = listen(fd, SOMAXCONN);
if(ret == -1){
	printf("listen failed!, errno=%d",errno);
}
struct sockaddr_in clientaddr;
socklen_t clientaddrlen = sizeof(clientaddr);
int afd = accept(fd, (struct sockaddr*) &clientaddr, &clientaddrlen);
if(afd != -1){
	printf("A client connected!,ip=%s,port=%d\n",inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
	char buf[256] = {0};
	ret = recv(afd, buf, 256, 0);
	if(ret > 0){
		printf("recv %d byte:%s\n",ret,buf);
	}
	else if(ret == -1){
		printf("recv error!,errno=%d\n",errno);
	}
	else{
		printf("client disconnect!\n");
	}	
}
else{
	printf("accept failed!,errno=%d\n",errno);
	close(fd);
}
}
  • 2.客户端
#include
#include
#include
#include
#include
#include 
#include
#include

int main(int argc, char* argv[]){
if(argc != 3){
	printf("please input IP port\n");
}
char* ip = argv[1];
unsigned int port = atoi(argv[2]);
int fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0){
	printf("create socket failed!, errno=%d\n",errno);
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip);
addr.sin_port = htons(port);

int ret;
ret = connect(fd, (struct sockaddr*) &addr, sizeof(addr));
if(ret == -1){
	printf("connect failed!, errno=%d",errno);
}
else{
	char *buf = "hello, I am a client!";
	ret = send(fd, buf, strlen(buf), 0);
	if(ret > 0){
		printf("send %d byte:%s\n",ret,buf);
	}
	else if(ret == -1){
		printf("send error!,errno=%d\n",errno);
	}
	else{
		printf("server disconnect!\n");
	}
}
	
}
  • 结果展示

你可能感兴趣的:(Linux,socket)