2019篮球世界杯投注官网socket读取Linux服务器上的

作者: win10  发布:2019-08-02

从Linux客户机上读取另一台Linux服务器上点名目录下的文书,服务器端代码和客户端代码实现如下:

#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define BUFFER 800
#define SERV_PORT 3333
int main()
{
int sockfd,n;
socklen_t len;
socklen_t src_len;
struct sockaddr_in servaddr, cliaddr;
char msg[BUFFER];
struct timeval tm;
fd_set rd_fd, wr_fd, ex_fd;
sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */
/* init servaddr */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
/* bind address and port to socket */
if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
{
perror("bind error");
exit(1);
}
src_len = sizeof(cliaddr);
FD_ZERO(&rd_fd);
FD_SET(sockfd, &rd_fd);
tm.tv_sec = 0;
tm.tv_usec = 0;
while(1)
{
FD_ZERO(&rd_fd);
FD_SET(sockfd, &rd_fd);
tm.tv_sec = 1;
tm.tv_usec = 0;
if(select(sockfd 1, &rd_fd, NULL, NULL, &tm) <= 0){
continue;
}
else
{
printf("%d, %d/n", tm.tv_sec, tm.tv_usec);
printf("there is data/n");
if(recvfrom(sockfd, msg, BUFFER, 0, (struct sockaddr *)&cliaddr, &src_len)< 0)
{
perror("receive error!/n");
exit(0);
}
printf("%s/n", msg);
sleep(5);
}
}
return 0;
}

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

 

#define SERVER_PORT 20000 // define the defualt connect port id
#define LENGTH_OF_LISTEN_QUEUE 10 //length of listen queue in server
#define BUFFER_SIZE 255
#define LINE_LEN 60  // in the file, define one line's length
#define WELCOME_MESSAGE "welcome to connect the server. "

早已写过如上那样的代码,实行时怎么也得不到预想的结果!
后来,仔细看man select
On success, select() and pselect() return the number of file descriptors contained in the three returned descriptor sets(that  is,  the  total  number  of  bits  that are set in readfds, writefds, exceptfds) which may be zero if the timeout expires before anything interesting happens.  On error, -1 is returned, and errno is set appropriately;  the  sets  and timeout become undefined, so do not rely on their contents after an error. 
原本第一遍select的时候出错了,导致rd_fd和tm(主要是rd_fd)的值 become undefined,进而导致随后的select调用的失败!
化解办法:
在1处(while循环内的始发),增添如下代码:
FD_ZERO(&rd_fd);
FD_SET(sockfd, &rd_fd);
tm.tv_sec = 1;
tm.tv_usec = 0;

int main(int argc, char **argv)
{
 int servfd,clifd;
 char s[LINE_LEN];
 struct sockaddr_in servaddr,cliaddr;

 if ((servfd = socket(AF_INET,SOCK_STREAM,0)) < 0){
  printf("create socket error!n");
  exit(1);
 }

 bzero(&servaddr,sizeof(servaddr));
 servaddr.sin_family = AF_INET;
 servaddr.sin_port = htons(SERVER_PORT);
 servaddr.sin_addr.s_addr = htons(INADDR_ANY);

 if (bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0){
  printf("bind to port %d failure!n",SERVER_PORT);
  exit(1);
 }

 if (listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0){
  printf("call listen failure!n");
  exit(1);
 }
 
 while(1){ //server loop will nerver exit unless any body kill the process 
  char buf[BUFFER_SIZE];
  long timestamp;
  socklen_t length = sizeof(cliaddr);

  clifd = accept(servfd,(struct sockaddr*)&cliaddr,&length);
  if (clifd < 0){
   printf("error comes when call accept!n");
   break;
  }
            
  strcpy(buf,WELCOME_MESSAGE);        

  printf("from client,IP:%s,Port:%dn",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
  timestamp = time(NULL);
  strcat(buf,"timestamp in server:");
  strcat(buf,ctime(×tamp));
 
  // read file rule.txt
  FILE* stream_src = fopen( "rule.txt", "r " );
  if( stream_src==NULL ){
   printf("the file rule.txt is not opened correctly!n");
   return 1;
  }
 
  while( fgets(s,sizeof(s),stream_src) ){ // 按行读取
   printf( "%s", s );
   strcat( buf,s );
  }
 
  // send file's content to client
  send(clifd,buf,BUFFER_SIZE,0);
  close(clifd);           
 }

 close(servfd);
 return 0;
}

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

#define SERVER_PORT 20000 // define the defualt connect port id
#define CLIENT_PORT ((20001 rand())e536) // define the defualt client port as a random port
#define BUFFER_SIZE 255
#define REUQEST_MESSAGE "welcome to connect the server.n"

void usage(char *name)
{
 printf("usage: %s IpAddrn",name);
}

int main(int argc, char **argv)
{   
 int servfd,clifd,length = 0;
 struct sockaddr_in servaddr,cliaddr;
 socklen_t socklen = sizeof(servaddr);
 char buf[BUFFER_SIZE];     

 if (argc < 2){
  usage(argv[0]);
  exit(1);
 }
 
 if ((clifd = socket(AF_INET,SOCK_STREAM,0)) < 0){
  printf("create socket error!n");
  exit(1);
 }

 srand(time(NULL));//initialize random generator
 bzero(&cliaddr,sizeof(cliaddr));
 cliaddr.sin_family = AF_INET;
 cliaddr.sin_port = htons(CLIENT_PORT);
 cliaddr.sin_addr.s_addr = htons(INADDR_ANY);
2019篮球世界杯投注官网, 
 bzero(&servaddr,sizeof(servaddr));
 servaddr.sin_family = AF_INET;
 inet_aton(argv[1],&servaddr.sin_addr);
 servaddr.sin_port = htons(SERVER_PORT);
 //servaddr.sin_addr.s_addr = htons(INADDR_ANY);

 if (bind(clifd,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0){
  printf("bind to port %d failure!n",CLIENT_PORT);
  exit(1);
 }

 if (connect(clifd,(struct sockaddr*)&servaddr, socklen) < 0){
  printf("can't connect to %s!n",argv[1]);
  exit(1);
 }    

 length = recv(clifd,buf,BUFFER_SIZE,0);
 if (length < 0){
  printf("error comes when recieve data from server %s!",argv[1]);
  exit(1);
 }

 printf("from server %s :nt%s ",argv[1],buf);
    
 close(clifd);
 return 0;
}

2019篮球世界杯投注官网 1

本文由篮球世界杯投注-2019篮球世界杯投注官网发布于win10,转载请注明出处:2019篮球世界杯投注官网socket读取Linux服务器上的

关键词: LINUX