套接字

服务器

#include<stdio.h>

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

int port=8018;

int main(void)
{
  struct sockaddr_in sin;
  struct sockaddr_in pin;
  int mysock;
  int tempsock;
  int addrsize;
  char str[100],str1[20],str2[20],str3[20];
  char buf[100];
  int i,len1,len2;
  float c;
  /*建立socket*/
  mysock=socket(AF_INET,SOCK_STREAM,0);
  if(mysock==-1)
  {
   perror("call to socket");
   exit(1);
  }
  /*建立IPv4地址*/
  bzero(&sin,sizeof(sin));
  sin.sin_family=AF_INET;
  sin.sin_addr.s_addr=htonl(INADDR_ANY);
  sin.sin_port=htons(port);
  /*绑定socket*/
  if(bind(mysock,(struct sockaddr *)&sin,sizeof(sin))==-1)
  {
  perror("call to bind");
  exit(1);
  }
  /*监听socket端*/
  if(listen(mysock,20)==-1)
  {
   perror("call to listen");
   exit(1);
  }
  printf("accepting connections...\n");

  while(1)
  {
     /*接受client端连接*/
    addrsize=sizeof(struct sockaddr_in);
    tempsock=accept(mysock,(struct sockaddr *)&pin,&addrsize);
    if(tempsock==-1)
    {
      perror("call to accept");
      exit(1);
    }
   /*接受client端传来的信息*/
   len1=recv(tempsock,str,100,0);
   printf("\nthe quantity of words are:%d\n",len1);
   str[len1]=0;

   printf("received from client:%s\n",str);

   if(len1>0)
   {
    strcpy(str1,strtok(str," "));
    printf("the first string is:%s\n",str1);
    strcpy(str2,strtok(NULL," "));
    printf("the second string is:%s\n",str2);
    strcpy(str3,strtok(NULL," "));
    printf("the third string is:%s\n",str3);
    c=atof(str3)*1.05;
    sprintf(buf,"the number of commodity is %s\n the name of commodity is %s\n the tax is %.2f\n",str1,str2,c);
   }
   /*将处理过的信息返回给client端*/
   len2=strlen(buf);
   if(send(tempsock,buf,len2,0)==-1)
   {
     perror("call to send");
    exit(1);
   }
    close(tempsock);
  }
   return 0;
}

客户端

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

int port=8018;

int main(int argc,char *argv[])
{
  struct sockaddr_in pin;
  int mysock;
  char buf[8192];
  char *str="A001 tv 20000.00";
 
  if(argc<2)
  {
   printf("the ways of use:client string \n");
   printf("the default string\n");
  }else{
   str=argv[1];
  }
   /*建立server IPv4地址*/
   bzero(&pin,sizeof(pin));
   pin.sin_family=AF_INET;
   pin.sin_addr.s_addr=inet_addr("127.0.0.1");
   pin.sin_port=htons(port);
   /*建立socket*/
   mysock=socket(AF_INET,SOCK_STREAM,0);
   if(mysock==-1)
   {
    perror("call to socket");
    exit(1);
   }
   /*连接server*/
   if(connect(mysock,(void *)&pin,sizeof(pin))==-1)
   {
     perror("call to connect");
     exit(1);
   }
   /*将str字符串传给server*/
   printf("sending message %s to server...\n",str);
   if(send(mysock,str,strlen(str),0)==-1)
   {
     perror("error in send\n");
     exit(1);
   }
   /*接受server回传的信息*/
   if(recv(mysock,buf,8192,0)==-1)
   {
     perror("error in receiving\n");
     exit(1);
   }
   printf("\nresponse from server:\n\n%s\n",buf);
   /*关闭与server的连接*/
   close(mysock);
   return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值