服务器不接受来自客户端的连接以从文件读取数据

时间:2014-11-12 11:01:05

标签: c linux sockets

我想从Linux上的客户端应用程序读取Server上的文件。我在下面提到了服务器和客户端应用程序。

在客户端,我打开一个文件(这是我的要求所必需的)。 客户端接收我在文件上写入的数据。现在,我想将收到的数据从客户端发送到服务器端,并将回送给客户端。

我遇到服务器接受客户端连接的问题。我很高兴有一个解决方案,我完全犯了错误。我也很感激如果我得到代码的解决方案。提前谢谢。

Server.C

#include<stdio.h>

#define BufferLength 100

/* Server port number */
#define SERVPORT 3389

int main(int argc, char *argv[])
{

/* Variable and structure definitions. */
int sd, sd2, rc, length = sizeof(int);
int totalcnt = 0, on = 1;
char temp;
char buffer[BufferLength];
//char data[BufferLength];
struct sockaddr_in serveraddr;
struct sockaddr_in their_addr;

fd_set read_fd;
struct timeval timeout;
timeout.tv_sec = 15;
timeout.tv_usec = 0;

/*To read xillybus*/
//  FILE *fp;
//  fp = fopen("/dev/xillybus_read_8", "r");
//  if(fp==NULL)
//  {
//      printf("Error file open\n");
//      exit(-1);
//  }
//  else{
//      printf("File found %s\n", fp);
//  }


/* Get a socket descriptor */

if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
    perror("Server-socket() error");
    exit (-1);
}
else
{
    printf("Server-socket() is OK\n");
}


/* Allow socket descriptor to be reusable */

if((rc = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) < 0)
{
    perror("Server-setsockopt() error");
    close(sd);
    exit (-1);
}
else
{
    printf("Server-setsockopt() is OK\n");
}

/* bind to an address */
memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVPORT);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
printf("Using %s, listening at %d\n", inet_ntoa(serveraddr.sin_addr), SERVPORT);



if((rc = bind(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
{
    perror("Server-bind() error");
    close(sd);
    exit(-1);
}

else
{
    printf("Server-bind() is OK\n");
}



/* Up to 10 clients can be queued */

if((rc = listen(sd, 10)) < 0)
{
    perror("Server-listen() error");
    close(sd);
    exit (-1);
}
else
{
    printf("Server-Ready for client connection...\n");
}


/* accept() the incoming connection request. */

int sin_size = sizeof(struct sockaddr_in);


if((sd2 = accept(sd, (struct sockaddr *)&their_addr, &sin_size)) < 0)
{

    perror("Server-accept() error");
    close(sd);
    exit (-1);
}
else
{

    printf("Server-accept() is OK\n");
}

/*client IP*/
    printf("Server-new socket, sd2 is OK...\n");
    printf("Got connection from the  client: %s\n", inet_ntoa(their_addr.sin_addr));

/* Wait for up to 15 seconds on */
/* select() for data to be read. */
FD_ZERO(&read_fd);
FD_SET(sd2, &read_fd);
rc = select(sd2+1, &read_fd, NULL, NULL, &timeout);

if((rc == 1) && (FD_ISSET(sd2, &read_fd)))
{
/* Read data from the client. */
totalcnt = 0;

while(totalcnt < BufferLength)
{


/* read() from client */
rc = read(sd2, &buffer[totalcnt], (BufferLength - totalcnt));
if(rc < 0)
{
    perror("Server-read() error");
    close(sd);
    close(sd2);
    exit (-1);
}

else if (rc == 0)
{
    printf("Client program has issued a close()\n");
    close(sd);
    close(sd2);
    exit(-1);
}

else
{
    totalcnt += rc;
    printf("Server-read() is OK\n");
}

}
}
else if (rc < 0)
{
    perror("Server-select() error");
    close(sd);
    close(sd2);
    exit(-1);
}

/* rc == 0 */
else
{
    printf("Server-select() timed out.\n");
    close(sd);
    close(sd2);
    exit(-1);
}

/* Shows the data */
    printf("Received data from the client: %s\n", buffer);
    printf("Server-Echoing back to client...\n");

/*Write function*/
rc = write(sd2, buffer, totalcnt);
if(rc != totalcnt)
{
    perror("Server-write() error");
    rc = getsockopt(sd2, SOL_SOCKET, SO_ERROR, &temp, &length);

if(rc == 0)
{
    /* Print out the asynchronously */
    /* received error. */
    errno = temp;
    perror("SO_ERROR was: ");
}
else
{
    printf("Server-write() is OK\n");
}
close(sd);
close(sd2);
exit(-1);
}


/******************************************/
close(sd2);
close(sd);
exit(0);
return 0;
}

Client.c

#include<stdio.h>

#define BufferLength 100

#define SERVER "00.00.00.00"
/* Server's port number */

#define SERVPORT 3389

/* set the server name in the #define SERVER ... */

int main(int argc, char *argv[])
{
/* Variable and structure definitions. */
int sd, rc, length = sizeof(int);
struct sockaddr_in serveraddr;
char buffer[BufferLength];
char server[255];
char temp;
int totalcnt = 0;
struct hostent *hostp;

//char data[500] = "Hello ravi !!! "; // writes data to the server


/*To open directory and read the xillybus*/

FILE *fp;
char s[100];
fp = fopen("/dev/xillybus_read_8", "r");
if(!fp)
    return 1;
while(fgets(s,100,fp)!=NULL){
    printf("%s ", s);
}
//fclose(fp);

/******************************************/
/* get a socket descriptor */

if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
    perror("Client-socket() error");
    exit(-1);
}
else
{
    printf("Client-socket() OK\n");
}
/*If the server hostname is supplied*/
if(argc > 1)
{
/*Use the supplied argument*/
    strcpy(server, argv[1]);
    printf("Connecting to the server %s, port %d ...\n", server, SERVPORT);
}
else
{
/*Use the default server name or IP*/
strcpy(server, SERVER);
memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVPORT);
}

if((serveraddr.sin_addr.s_addr = inet_addr(server)) == (unsigned long)INADDR_NONE)
{

/* get host address */
hostp = gethostbyname(server);
if(hostp == (struct hostent *)NULL)
{
    printf("HOST NOT FOUND --> ");
/* h_errno is usually defined */
/* in netdb.h */
    printf("h_errno = %d\n",h_errno);
    printf("---This is a client program---\n");
    printf("Command usage: %s <server name or IP>\n", argv[0]);
    close(sd);
    exit(-1);
}
memcpy(&serveraddr.sin_addr, hostp->h_addr, sizeof(serveraddr.sin_addr));
}


/* connect() to server. */

if((rc = connect(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
{
    perror("Client-connect() error");
    close(sd);
    exit(-1);
}
else
{
    printf("Connection established...\n");
    printf("Sending some string to the server %s...\n", server);
}
/* Write() some string to the server. */
//rc = write(sd, data, sizeof(data));

rc = write(sd,fp, sizeof(fp)); // here is my write function

if(rc < 0)
{
    perror("Client-write() error");
    rc = getsockopt(sd, SOL_SOCKET, SO_ERROR, &temp, &length);
if(rc == 0)
{
/* Print out the asynchronously received error. */
    errno = temp;
    perror("SO_ERROR was");
}
close(sd);
exit(-1);
}
else
{
    printf("Client-write() is OK\n");
    printf("String successfully sent \n");
    printf("Waiting the %s to echo back...\n", server);
}
totalcnt = 0;

/* Read data from the server. */
while(totalcnt < BufferLength)
{
rc = read(sd, &buffer[totalcnt], BufferLength-totalcnt); //&buffer[totalcnt],BufferLength-totalcnt
if(rc < 0)
{
    perror("Client-read() error");
    close(sd);
    exit(-1);
}
else if (rc == 0)
{
    printf("Server program has issued a close()\n");
    close(sd);
    exit(-1);
}
else
{
    totalcnt += rc;
}
}
    printf("Client-read() is OK\n");
    printf("Echoed data from the  server: %s\n", buffer); //data ->buffer

/* Close socket descriptor from client side. */
close(sd);
exit(0);
return 0;
}

1 个答案:

答案 0 :(得分:0)

在您的client.c程序中,您尝试向服务器发送FILE*。您发送的是客户端程序中文件描述符的地址,文件中包含的文本。

您只发送4或8个字节(取决于架构32或64位),而服务器在回显任何内容之前等待至少100个字节。所以这两个程序都完全按照您编程的方式完成 - 但不是您所期望的。

要发送文件的内容,请在char数组中读取文件并发送char数组。您可以迭代读取一个缓冲区的文件并发送缓冲区。但是没有魔法允许发送整个文件。