C语言技术网SOCKET相关笔记

// service.cpp

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc, char* argv[])
{
	if (argc!=2) {printf("Using:./server port\nExample: ./server 5005\n\n"); return -1;}
	
	//step1: create the server socket
	int listenfd;// create the server file descriptor
	if ((listenfd=socket(AF_INET, SOCK_STREAM, 0)) == -1) {perror("socket"); return -1;}

	//step2: bind the ip and port to the server socket
	struct sockaddr_in servaddr;
	memset(&servaddr, 0, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(atoi(argv[1]));
	
	if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0)
	{perror("bind\n"); close(listenfd); return -1;}
	
	//step3: turn on the socket
	if (listen(listenfd, 5) != 0)
	{perror("listen\n"); close(listenfd); return -1;}

	//step4: recevie the request from the client
	int clientfd; // we have to create a dedicated one for the client socket to "accept()" the socket from the server socket
	int socklen = sizeof(struct sockaddr_in);
	struct sockaddr_in clientaddr; // receive the addr from server socket
	clientfd=accept(listenfd, (struct sockaddr*)&clientaddr, (socklen_t*)&socklen); // attention: the last parametar is "socklen_t*"
	printf("the client (%s) is linked\n", inet_ntoa(clientaddr.sin_addr)); 
		// "inet_ntoa()" is a function to convert the addr to a string format 
	
	//step5:receive and reply the message from the external client
	char buffer[1024];
	while (1)
	{
		int iret;
		memset(buffer, 0, sizeof(buffer));
		if ((iret=recv(clientfd, buffer, sizeof(buffer), 0)) <= 0) {printf("iret=%d\n", iret); break;}
		printf("receive: %s\n", buffer);

		strcpy(buffer, "ok");
		if ((iret=send(clientfd, buffer, strlen(buffer), 0)) <= 0) {perror("send\n"); break;}
		printf("sent: %s\n", buffer);
	}
	close(listenfd); close(clientfd);
}

//client.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#include <netdb.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <arpa/inet.h>

int main(int argc, char* argv[])
{
	if (argc!=3){
		printf("Using:./client ip port\nExample:./client 127.0.0.1 5005\n\n");
		return -1;
	}
	
	//step1: creat the client socket
	int sockfd; // create the socket file descriptor
	if ((sockfd=socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{perror("socket"); return -1;}
	
	//step2: request to the server
	struct hostent *h; // get the ipadd from the programe argument
	if ((h=gethostbyname(argv[1])) == 0)
	{printf("gethostbyname failed\n"); close(sockfd); return -1;}

	struct sockaddr_in servaddr;
	memset(&servaddr, 0, sizeof(servaddr));
	
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(atoi(argv[2]));
	memcpy(&servaddr.sin_addr, h->h_addr, h->h_length);
	
	// ready to request
	if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) 
	{perror("connet\n"); close(sockfd); return -1;}

	char buffer[1024];
	
	//step3&4: send & receive the message
	for (int ii=0; ii<3; ++ii)
	{
		int iret; // cache the return state
		memset(buffer, 0, sizeof(buffer));
		sprintf(buffer, "this is the %d th supergirl, the order number is %d", ii+1, ii+1);
		
		if ((iret=send(sockfd, buffer, strlen(buffer), 0)) <= 0) 
			// when sending a message, we send a string into the socket, so we use the "strlen()" as the third argument
		{perror("send"); break;} 
		printf("send: %s\n", buffer);
		
		memset(buffer, 0, sizeof(buffer));
		if ((iret=recv(sockfd, buffer, sizeof(buffer), 0)) <= 0)
			// when receiving a message, we got a binary data from the socket, so we use the "sizeof()" as the third argument
		{perror("recv\n"); break;}
		printf("recv: %s\n", buffer);
		
	}
	close(sockfd);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值