当前位置:  技术问答>linux和unix

请Winsoket高手进来帮Me看看socket通信问题

    来源: 互联网  发布时间:2015-09-06

    本文导语:  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #define STDIN 0 #define STDOUT 1 #define STDERR 2 #define PORT 1119 #define BUFFER_SIZE 1024 int main() {    int socket_des;    int ...

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define STDIN 0
#define STDOUT 1
#define STDERR 2
#define PORT 1119
#define BUFFER_SIZE 1024

int main()
{
   int socket_des;
   int address_len;
   int real_len;
   int real_send_len;
   int real_receive_len;
   char sendstr[]="Welcome to me!n";
   char server_input[BUFFER_SIZE];
   char client_message[BUFFER_SIZE];
   struct timeval tv;
   struct sockaddr_in address;
   fd_set readfds;

   tv.tv_sec =20;
   tv.tv_usec =500000;//请问这是什么意识
   

   socket_des =socket(AF_INET,SOCK_STREAM,0);

   address.sin_family =AF_INET;
   address.sin_addr.s_addr =htonl(INADDR_ANY);
   address.sin_port =htons(PORT);
   bzero(address.sin_zero,8);//请问这是什么意识
   address_len =sizeof(address);//请问这是什么意识

   
   bind(socket_des, (struct sockaddr *)&address,address_len);

   listen(socket,5);

   system("clear");
   while(1)
   {
      int len;
      pid_t pid;
      int client_sock_des;
      struct sockaddr_in client_address;
      len =sizeof(client_address);
      
      
      client_sock_des =accept(socket_des,(struct sockaddr *)&client_address, &len);
      if((pid=fork())==0)//请问这是什么意识
      {
 close(socket_des);
 printf("111  Get connection from client %sn",inet_ntoa(client_address.sin_addr));为什么这行老是执行啊
 // char sendstr[]="Welcome connect to men";
 real_send_len =send(client_sock_des,sendstr,strlen(sendstr),0);
 while(1)
 {
    FD_ZERO(&readfds);
    FD_SET(STDIN,&readfds);
    select(client_sock_des+1,&readfds,NULL,NULL,&tv);
    if(FD_ISSET(STDIN,&readfds))
    {// Send to Server
real_len = read(STDIN,server_input,sizeof(server_input));
server_input[real_len]='';
real_send_len = send(client_sock_des,server_input,real_len,0);
// need some process here
        printf("Send to Client::%s",server_input);
    }
    else if(FD_ISSET(client_sock_des,&readfds))
    {// receive and send to output
               real_receive_len = recv(client_sock_des,client_message,BUFFER_SIZE,0);
       if(real_receive_len==0)
       {
   exit(0);
       }
       else
       {
   client_message[real_receive_len]='';
   printf(" Frome client ::%s",client_message);    
       }
    }
    else
    {
close(client_sock_des);
printf("time outn");
exit(1);
    }  
 }//in  Wend
         close(client_sock_des);
         exit(0);
     }// End if
     close(client_sock_des);
  }// Wend
  exit(0);
  return 0;
}


请问我怎么用VB/VC/C++在Windows2k下进行通信

|
int main()
{
   int socket_des;
   int address_len;
   int real_len;
   int real_send_len;
   int real_receive_len;
   char sendstr[]="Welcome to me!n";
   char server_input[BUFFER_SIZE];
   char client_message[BUFFER_SIZE];
   struct timeval tv;
   struct sockaddr_in address;
   fd_set readfds;

   tv.tv_sec =20;
   tv.tv_usec =500000;//请问这是什么意识   500000微秒 即0.5秒
   

   socket_des =socket(AF_INET,SOCK_STREAM,0);

   address.sin_family =AF_INET;
   address.sin_addr.s_addr =htonl(INADDR_ANY);
   address.sin_port =htons(PORT);
   bzero(address.sin_zero,8);//请问这是什么意识        清零 从address.sin_zero开始8字节
   address_len =sizeof(address);//请问这是什么意识  取address结构大小

   
   bind(socket_des, (struct sockaddr *)&address,address_len);

   listen(socket,5);

   system("clear");
   while(1)
   {
      int len;
      pid_t pid;
      int client_sock_des;
      struct sockaddr_in client_address;
      len =sizeof(client_address);
      
      
      client_sock_des =accept(socket_des,(struct sockaddr *)&client_address, &len);
      if((pid=fork())==0)//请问这是什么意识        fork 创建进程 ==0 子进程
      {
 close(socket_des);
 printf("111  Get connection from client %sn",inet_ntoa(client_address.sin_addr));为什么这行老是执行啊       主进程一直未退出 while(1)
 // char sendstr[]="Welcome connect to men";
 real_send_len =send(client_sock_des,sendstr,strlen(sendstr),0);
 while(1)
 {
    FD_ZERO(&readfds);
    FD_SET(STDIN,&readfds);
    select(client_sock_des+1,&readfds,NULL,NULL,&tv);
    if(FD_ISSET(STDIN,&readfds))
    {// Send to Server
real_len = read(STDIN,server_input,sizeof(server_input));
server_input[real_len]='';
real_send_len = send(client_sock_des,server_input,real_len,0);
// need some process here
        printf("Send to Client::%s",server_input);
    }
    else if(FD_ISSET(client_sock_des,&readfds))
    {// receive and send to output
               real_receive_len = recv(client_sock_des,client_message,BUFFER_SIZE,0);
       if(real_receive_len==0)
       {
   exit(0);
       }
       else
       {
   client_message[real_receive_len]='';
   printf(" Frome client ::%s",client_message);    
       }
    }
    else
    {
close(client_sock_des);
printf("time outn");
exit(1);
    }  
 }//in  Wend
         close(client_sock_des);
         exit(0);
     }// End if
     close(client_sock_des);
  }// Wend
  exit(0);
  return 0;
}

还要多学习

    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3