简单的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

相关推荐
szuzhan.gy8 分钟前
DS查找—二叉树平衡因子
数据结构·c++·算法
一只码代码的章鱼39 分钟前
排序算法 (插入,选择,冒泡,希尔,快速,归并,堆排序)
数据结构·算法·排序算法
青い月の魔女1 小时前
数据结构初阶---二叉树
c语言·数据结构·笔记·学习·算法
林的快手2 小时前
209.长度最小的子数组
java·数据结构·数据库·python·算法·leetcode
千天夜2 小时前
多源多点路径规划:基于启发式动态生成树算法的实现
算法·机器学习·动态规划
从以前2 小时前
准备考试:解决大学入学考试问题
数据结构·python·算法
.Vcoistnt2 小时前
Codeforces Round 994 (Div. 2)(A-D)
数据结构·c++·算法·贪心算法·动态规划
ALISHENGYA3 小时前
全国青少年信息学奥林匹克竞赛(信奥赛)备考实战之分支结构(实战训练三)
数据结构·c++·算法·图论
我码玄黄5 小时前
正则表达式优化之算法和效率优化
前端·javascript·算法·正则表达式
Solitudefire6 小时前
蓝桥杯刷题——day9
算法·蓝桥杯