简单的socket TCP编程

1、server.c

cpp 复制代码
#include<stdio.h>
#include <sys/types.h>          
#include <sys/socket.h> //man 2 socket include
//#include<linux/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>

//int socket(int domain, int type, int protocol);
//int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
//uint32_t htonl(uint32_t hostlong);
//uint16_t htons(uint16_t hostshort);
//uint32_t ntohl(uint32_t netlong);
// uint16_t ntohs(uint16_t netshort);
//int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

#if 0
/* Internet address. */
struct in_addr {
	__be32  s_addr;
};

struct sockaddr_in {
	__kernel_sa_family_t  sin_family;     /* Address family               */
	__be16                sin_port;       /* Port number                  */
	struct in_addr        sin_addr;       /* Internet address             */

	/* Pad to size of `struct sockaddr'. */
	unsigned char         __pad[__SOCK_SIZE__ - sizeof(short int) -
		sizeof(unsigned short int) - sizeof(struct in_addr)];
};
#endif

int main(int argc ,char **argv)
{
	int s_fd;
	int c_fd;
	int n_read;
	char readbuf[128];
	char message[128] = {0};
	struct sockaddr_in s_addr;
	struct sockaddr_in c_addr;
	memset(&s_addr,0,sizeof(struct sockaddr_in));

	//1.create socket
	s_fd = socket(AF_INET,SOCK_STREAM,0);
	if(s_fd == -1)
	{
		perror("socket");
		exit(-1);
	}	

	//2.bind
	s_addr.sin_family = AF_INET;
	s_addr.sin_port   = htons(atoi(argv[2]));//put port to net endian
	inet_aton(argv[1],&s_addr.sin_addr);//jiang server ip trans to net endian
	bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));//struct sockaddr_in replace ,so need trans pointer

	//3.listen
	listen(s_fd,10);//10 inluce had 3 wo shou he no 3 wo shou

	//4.accept
	int c_len = sizeof(struct sockaddr_in);


	while(1)
	{
		c_fd = accept( s_fd, (struct sockaddr *)&c_addr, &c_len);//accept client information
		if(c_fd == -1)
		{
			perror("accept");
			exit(-1);
		}

		printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
		if(fork() == 0)
		{
			while(1)
			{

				//5.read
				memset(readbuf,0,sizeof(readbuf));
				n_read = read(c_fd,readbuf,128);
				if(n_read == -1)
				{
					perror("read");
				}
				else
				{
					printf("client message is :size[%d] ,%s\n",n_read,readbuf);
					//6.write
					memset(message,0,sizeof(message));
					printf("server input:");
					fgets(message,sizeof(message),stdin);
					write(c_fd,message,strlen(message));
				}
			}
		}

	}
	return 0;
}

2、client.c

cpp 复制代码
#include<stdio.h>
#include <sys/types.h>          
#include <sys/socket.h> //man 2 socket include
//#include<linux/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>


//int socket(int domain, int type, int protocol);
//int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
//uint32_t htonl(uint32_t hostlong);
//uint16_t htons(uint16_t hostshort);
//uint32_t ntohl(uint32_t netlong);
// uint16_t ntohs(uint16_t netshort);
//int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
//int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);

#if 0
/* Internet address. */
struct in_addr {
	__be32  s_addr;
};

struct sockaddr_in {
	__kernel_sa_family_t  sin_family;     /* Address family               */
	__be16                sin_port;       /* Port number                  */
	struct in_addr        sin_addr;       /* Internet address             */

	/* Pad to size of `struct sockaddr'. */
	unsigned char         __pad[__SOCK_SIZE__ - sizeof(short int) -
		sizeof(unsigned short int) - sizeof(struct in_addr)];
};
#endif

int main(int argc ,char **argv)
{
	int c_fd;
	int n_read;
	int n_write;
	char readbuf[128];
	char message[128] = {0};
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));

	//1.create socket
	c_fd = socket(AF_INET,SOCK_STREAM,0);
	if(c_fd == -1)
	{
		perror("socket");
		exit(-1);
	}	

	//2.connect
	c_addr.sin_family = AF_INET;
	c_addr.sin_port   = htons(atoi(argv[2]));//put port to net endian
	inet_aton(argv[1],&c_addr.sin_addr);//jiang server ip trans to net endian
	if(connect(c_fd, (struct sockaddr *)&c_addr, sizeof(struct sockaddr)) == -1)
	{
		perror("connect");
		exit(-1);
	}	

	while(1)
	{
		//6.write
		memset(message,0,sizeof(message));
		memset(readbuf,0,sizeof(readbuf));

		printf("client input: ");
		fgets(message,sizeof(message),stdin);
		write(c_fd,message,strlen(message));

		n_read = read(c_fd,readbuf,128);
		if(n_read == -1)
		{
			perror("read");
		}
		else
		{
			printf("message from server :size[%d] %s\n",n_read,readbuf);
		}
	}



	return 0;
}

查看结构体

grep "struct test {" * -nir //n带行号 i不区分大小写 r 递归

vi sys/in.h +183

前期验证socket :

(1)ifconfig :查看虚拟机ip 192.168.124.7

电脑cmd : ping 192.168.124.7 是否能通

电脑cmd : telnet 192.168.124.7 8989

程序运行方式

gcc client.c -o client

gcc server.c -o server

./server 192.168.124.7 8988

./client 192.168.124.7 8988

相关推荐
视觉小萌新3 分钟前
VScode+opencv——关于opencv多张图片拼接成一张图片的算法
vscode·opencv·算法
2的n次方_14 分钟前
二维费用背包问题
java·算法·动态规划
simple_ssn42 分钟前
【C语言刷力扣】1502.判断能否形成等差数列
c语言·算法·leetcode
寂静山林1 小时前
UVa 11855 Buzzwords
算法
Curry_Math1 小时前
LeetCode 热题100之技巧关卡
算法·leetcode
ahadee1 小时前
蓝桥杯每日真题 - 第10天
c语言·vscode·算法·蓝桥杯
军训猫猫头2 小时前
35.矩阵格式的一到一百数字 C语言
c语言·算法
Mr_Xuhhh3 小时前
递归搜索与回溯算法
c语言·开发语言·c++·算法·github
SoraLuna3 小时前
「Mac玩转仓颉内测版12」PTA刷题篇3 - L1-003 个位数统计
算法·macos·cangjie
爱吃生蚝的于勒5 小时前
C语言内存函数
c语言·开发语言·数据结构·c++·学习·算法