其他分享
首页 > 其他分享> > 网络套接字socket

网络套接字socket

作者:互联网

socket套接字

image
image

网络字节序

小端法存储,高位存高地址,地位存低地址。网络字节序是大端法:
image
image
image
image
image
image

sockaddr地址结构

image
image
image

socket模型

image
客户端和服务端进行通信的时候会有3个socket
image

socket()和bind()函数

image
image

listen()和accept()函数

image
accept()阻塞等待客户端进行连接,成功的话,返回一个与客户端成功建立连接的socket文件描述符
image

connect()函数

image

总结

image

server实现

/*************************************************************************
  > File Name: server.c
  > Author: shaozheming
  > Mail: 957510530@qq.com
  > Created Time: 2022年03月03日 星期四 19时32分56秒
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define SERVER_PORT 9527

void sys_err(const char *str)
{
	perror(str);
	exit(1);
}

int main(int argc, char* argv[])
{
	int lfd = 0, cfd = 0;

	int ret;
	char buf[BUFSIZ], client_ip[BUFSIZ]; //4096

	struct sockaddr_in serv_addr, clit_addr;
	socklen_t clit_addr_len;

	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(SERVER_PORT);
	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

	lfd = socket(AF_INET, SOCK_STREAM, 0);//TCP
	if(lfd == -1){
		sys_err("socket error!\r\n");
	}

	bind(lfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));

	listen(lfd, 128);

	clit_addr_len = sizeof(clit_addr);
	cfd = accept(lfd, (struct sockaddr *)&clit_addr, &clit_addr_len);	
	if(cfd == -1){
		sys_err("accept error!\r\n");
	}
	printf("client ip: %s, port:%d\n", 
			inet_ntop(AF_INET, &clit_addr.sin_addr.s_addr, client_ip, sizeof(client_ip)), 
			ntohs(clit_addr.sin_port));

	while(1){
		ret = read(cfd, buf, sizeof(buf));
		write(STDOUT_FILENO, buf, ret);

		for(int i = 0; i < ret; ++i){
			buf[i] = toupper(buf[i]); //小写转大写
		}

		write(cfd, buf, ret);
	}

	close(lfd);
	close(cfd);

	return 0;
}

image

client实现

/*************************************************************************
  > File Name: server.c
  > Author: shaozheming
  > Mail: 957510530@qq.com
  > Created Time: 2022年03月03日 星期四 19时32分56秒
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define SERVER_PORT 9527

void sys_err(const char *str)
{
	perror(str);
	exit(1);
}

int main(int argc, char* argv[])
{
	int cfd;
	int count = 10;
	int ret;
	char buf[BUFSIZ];//4096

	struct sockaddr_in serv_addr;

	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(SERVER_PORT);
	inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr.s_addr);

	cfd = socket(AF_INET, SOCK_STREAM, 0);//TCP
	if(cfd == -1){
		sys_err("socket error!\r\n");
	}

	ret = connect(cfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
	if(ret != 0){
		sys_err("connect error!\r\n");
	}
	
	while(--count){
		write(cfd, "hello", 5);
		ret = read(cfd, buf, sizeof(buf));
		write(STDOUT_FILENO, buf, ret);
	}

	close(cfd);

	return 0;
}

标签:serv,addr,cfd,网络,ret,接字,include,buf,socket
来源: https://www.cnblogs.com/yoshinb/p/15961656.html