一个简单的客户端和服务端网络连接程序

这是一个简单的TCP/IP网络通信程序,包括客户端和服务器端代码。客户端从服务器接收数据并写入文件,服务器读取文件内容并发送给客户端。代码使用C语言编写,涉及socket编程,包括socket创建、bind、listen、accept和connect等函数的使用。
摘要由CSDN通过智能技术生成

客户端
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
int main()
{
  int sockfd,len;
  struct sockaddr_in address;  
  int result;
  FILE*fd= fopen("sunrise.jpg","wb");
  char recvs[MAXSIZE];
  sockfd=socket(AF_INET,SOCK_STREAM,0);
  address.sin_family=AF_INET;
  address.sin_addr.s_addr=inet_addr("127.0.0.1");
  address.sin_port=9734;
  len=sizeof(address);
  result=connect(sockfd,(struct sockaddr *)&address,len);
  if(result==-1){
  perror("oops:client1");
  exit(1);
  }
  int count;
  while(1){
  int count;
  count=read(sockfd,recvs,MAXSIZE);
  if(count==0)
  break;
  fwrite(recvs,1,MAXSIZE,fd);
}
close(sockfd);
fclose(fd);
return 0;
}


服务器
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXSIZE 100
int main()
{
  FILE *fd=fopen("Sunset.jpg","rb");
  int server_sockfd,client_sockfd;
  int server_len,client_len;
  struct sockaddr_in server_address;
  struct sockaddr_in client_address;
  server_sockfd=socket(AF_INET,SOCK_STREAM,0);
  server_address.sin_family=AF_INET;
  server_address.sin_addr.s_addr=inet_addr("127.0.0.1");
  server_address.sin_port=9734;
  server_len=sizeof(server_address);
  bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
  listen(server_sockfd,5);
  char ch[MAXSIZE];
  client_len=sizeof(client_address);
  client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,&client_len);
  while(!feof(fd)){
  fread(ch,1,MAXSIZE,fd);
  write(client_sockfd,ch,MAXSIZE);
}
  close(client_sockfd);
  fclose(fd);
  return 0;
}

 

 

 

 

/**
 * @file a.c
 * @brief
 * @author miracle, <miracle.taox@gmail.com>
 * @version 1.0
 * @date 2016-10-12
 */
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<unistd.h>
#include<pthread.h>

#define MAXSIZE    1024

void setHostIP();
int sendApp1();
int sendApp2();
int sendApp3();
#if 1
int main()
{
    pthread_t p_camera;
    pthread_attr_t attr_camera;
    pthread_attr_init(&attr_camera);
    pthread_attr_setdetachstate(&attr_camera,PTHREAD_CREATE_DETACHED);
    //setHostIP();
    if(pthread_create(&p_camera,&attr_camera,(void *)sendApp3,NULL) != 0)
    {
        printf("Net creat pthread err.");
        exit(1);
    }

    while(1)
    {
        sleep(1);
    }
    printf("main is over\n");
}
#endif
void setHostIP()
{
    char tbuf[128];

    system("/sbin/ifconfig eth0 down > /dev/null");
    system("/sbin/ifconfig ra0 down > /dev/null");
    //sprintf(tbuf,"/sbin/ifconfig eth1 hw ether 1234567890%d > /dev/null",bedno);//linzilin--下面这2个要同时搞
    //system(tbuf);

    system("/sbin/ifconfig ra0 up > /dev/null");
    system("killall wpa_supplicant &");
    system("/usr/sbin/wpa_supplicant -B -ira0 -c /etc/wpa_supplicant.conf -Dwext &");
    //system("iwlist eth1 scanning");
    sprintf(tbuf, "/sbin/ifconfig ra0 202.114.4.200 > /dev/null");//linzilin--这2个要同时搞 
    //sprintf(tbuf, "/sbin/ifconfig ra0 202.114.4.115 > /dev/null");//linzilin--这2个要同时搞 
    system(tbuf);
}

int sendApp3()
{
    int sockfd,new_fd;
 int nbytes;
    struct sockaddr_in server_addr;/*定义服务器端套接口数据结构 server_addr*/
    struct sockaddr_in client_addr;/*定义客户端套接口数据结构 client_addr*/
    int sin_size,portnumber;
    char buf[MAXSIZE];
   static char ServerIP[20]="192.168.1.43" ;
printf("00*************************************\n");
    /*服务器端开始建立socket描述符*/
    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
    {
        fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
        exit(1);
    }
    /*服务器端填充sockaddr结构*/
    bzero(&server_addr,sizeof(struct sockaddr_in));//先将套接口地址数据结构清零
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr((char *) ServerIP);;
    //server_addr.sin_port = htons(portnumber);
    server_addr.sin_port = htons(9999);
printf("11*************************************INADDR_ANY = %s\n",INADDR_ANY);
    /*调用bind函数绑定端口*/
    if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1)
    {
        fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
        exit(1);
    }
    /*端口绑定成功,监听sockfd描述符,同时处理的最大连接请求数为5*/
    if(listen(sockfd,5) == -1)
    {
        fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
        exit(1);
    }
printf("22*************************************\n");
    /*服务器阻塞,等待接收连接请求,直到客户程序发送连接请求*/
    while(1)
    {
        sin_size = sizeof(struct sockaddr_in);
printf("88888*************************************\n");
        /*调用accept 接受一个连接请求*/
        if((new_fd = accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size)) == -1)
        {
            fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
            exit(1);
        }
printf("23333*************************************\n");

        /*TCP 连接已建立,打印申请连接的客户机的IP地址*/
        fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
        /*提示用户输入将要发送的数据,长度小于缓冲区的长度,即1024字节*/
        printf("Connected successful,please input the message[<1024 bytes]:\n");

 

#if 1
while(1)
{
   if((nbytes = recv(new_fd, buf, MAXSIZE, 0)) < 0)
    {
        fprintf(stderr,"4Gethostname error\n");
        exit(1);
    }
    //buffer[nbytes] = '\0';
    printf("I have received:%s\n",buf);
    printf("nbytes = %d\n",nbytes);
if(nbytes == 25)
{
 int i = 0;
 for(i = 0; i < 53;i++)
{
 sprintf("buf[%d] = %x\n",i, buf[i]);
}
}
    memset(buf,0,sizeof(buf));  
}
 
#endif

    }
    close(sockfd);//服务器进程结束,关闭服务器端套接口
    exit(0);
}

 


int my_system(const char * cmd_line)
{
 FILE *fp;
 int res = 0;
 char buf[1024];
 if(NULL == cmd_line)
 { 
  printf("my_system cmd_line is NULL!\n");
  return -1;
 }

 if(NULL == (fp = popen(cmd_line, "r")))
 {
  perror("popen");
  printf("popen erro:%s\n", strerror(errno));
  return -1;
 }
 else
 {
  while(fgets(buf, sizeof(buf), fp))
  {
   printf("%s", buf);
  }
  
  if(res = pclose(fp) == -1)
  {
   printf("close popen file pointer fp error!\n");
   return res;
  }
  else if(0 == res)
  {
   return res;
  }
  else
  {
   printf("poen res is :%d\n", res);
   return res;
  }
 }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值