欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TCP socket 编程示例

程序员文章站 2022-03-18 19:14:51
...

本代码为老师提供的示例程序

一、TCP socket 编程模型

            TCP socket 编程示例

二、使用TCP协议实现一个简单的通讯程序

1.服务器端

#include<stdlib.h>
#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>

#define PORT 8900

int main(int argc,char** argv)
{
	struct sockaddr_in server;
	struct sockaddr_in client;
	int len;
	int port;
	int listend;
	int connectd;
	int sendnum;
	int opt;
	int recvnum;
	char send_buf[2048];
	char recv_buf[2048];


	port= PORT;
	memset(send_buf,0,2048);
	memset(recv_buf,0,2048);
	
    opt = SO_REUSEADDR;
      

    if (-1==(listend=socket(AF_INET,SOCK_STREAM,0)))
    {
	 perror("create listen socket error\n");
	 exit(1);
    }
    setsockopt(listend,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));


    memset(&server,0,sizeof(struct sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(port);

    if (-1==bind(listend,(struct sockaddr *)&server,sizeof(struct sockaddr)))
    {
	perror("bind error\n");
	exit(1);
    }

    if (-1==listen(listend,5))
    {
	perror("listen error\n");
	exit(1);
    }

    while (1)
    {
        if (-1==(connectd=accept(listend,(struct sockaddr*)&client,&len)))
    	{
		perror("create connect socket error\n");
		continue;
    	}

	
	sendnum = sprintf(send_buf,"hello,the guest from %s\n",inet_ntoa(client.sin_addr));
    if ( 0 >send(connectd,send_buf,sendnum,0))
	{
		perror("send error\n");
		close(connectd);
		continue;
	}
	

    if (0>(recvnum = recv(connectd,recv_buf,sizeof(recv_buf),0)))
	{
		perror("recv error\n");
		close(connectd);
		continue;
	}
	recv_buf[recvnum]='\0';

	printf ("the message from the client is: %s\n",recv_buf);

	if (0==strcmp(recv_buf,"quit"))
        {
		perror("the client break the server process\n");
		close(connectd);
		break;
	}

	sendnum = sprintf(send_buf,"%s\n",recv_buf);
  
    send(connectd,send_buf,sendnum,0);
  
	close(connectd);
	continue;
   }
    close(listend);
    return 0;
}

2.客户端

#include<stdlib.h>
#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>

#define PORT 8900

void print_usage(char * cmd)
{
	fprintf(stderr," %s usage:\n",cmd);
	fprintf(stderr,"%s IP_Addr [port]\n",cmd);

}


int main(int argc,char** argv)
{
	struct sockaddr_in server;
	int ret;
	int len;
	int port;
	int sockfd;
	int sendnum;
	int recvnum;
	char send_buf[2048];
	char recv_buf[2048];

	if ((2>argc)|| (argc >3))
	{
		print_usage(argv[0]);
		exit(1);

	}

    if (3==argc) 
    {
		port = atoi(argv[2]);
    }

    if (-1==(sockfd=socket(AF_INET,SOCK_STREAM,0)))
	{
		perror("can not create socket\n");
		exit(1);
	}

	memset(&server,0,sizeof(struct sockaddr_in));
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = inet_addr(argv[1]);
	server.sin_port = htons(port);

	if (0>(ret=connect(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))))
	{
		perror("connect error");
		close(sockfd);
		exit(1);

	}

	printf("what words do  you want to tell to server:\n");
	fgets(send_buf,2048,stdin);

	if (0>(len=send(sockfd,send_buf,strlen(send_buf),0)))
	{
		perror("send data error\n");
		close(sockfd);
		exit(1);

	}

	if (0>(len=recv(sockfd,recv_buf,2048,0)))
	{
		perror("recv data error\n");
		close(sockfd);
		exit(1);
	}
	
	recv_buf[len]='\0';

	printf("the message from the server is:%s\n",recv_buf);
	close(sockfd);

}

三、运行结果

TCP socket 编程示例

TCP socket 编程示例

相关标签: socket