栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言技术网SOCKET相关笔记

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言技术网SOCKET相关笔记

// service.cpp

#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 

int main(int argc, char* argv[])
{
	if (argc!=2) {printf("Using:./server portnExample: ./server 5005nn"); 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("bindn"); close(listenfd); return -1;}
	
	//step3: turn on the socket
	if (listen(listenfd, 5) != 0)
	{perror("listenn"); 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 linkedn", 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=%dn", iret); break;}
		printf("receive: %sn", buffer);

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

//client.cpp

#include 
#include 
#include 

#include 

#include 

#include 
#include 

#include 

int main(int argc, char* argv[])
{
	if (argc!=3){
		printf("Using:./client ip portnExample:./client 127.0.0.1 5005nn");
		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 failedn"); 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("connetn"); 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: %sn", 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("recvn"); break;}
		printf("recv: %sn", buffer);
		
	}
	close(sockfd);
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1022775.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号