开发者

Client/server sockets in c

I am currently trying to understand how to use sockets to make a client/server program in C. I have been reading various tutorials around the Internet in hopes to try and make a small echo server that can deal with multiple clients at the same time. Whenever the client sends the server a message, the server is supposed to echo is back to the client. The code that I created is a combination of both a tutorial from a lecture at school (which explained how to create the client and server) as well as an example I found on here on stackoverflow (which showed how to make the functions to actually echo the messages). I am hoping that someone can explain to me what I am missing to make this program work correctly. Here is the client code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>

char buf[80];
struct sockaddr myname;

void replyBack(FILE *fp, int sockfd) {
char sendline[1000], recvline[1000];
printf("Enter your echo: \n");
while(fgets(sendline,1000,stdin) != NULL) {
    write(sockfd,sendline,sizeof(sendline));
    if(read(sockfd,recvline,1000) == 0) {
        printf("str_cli: server terminated prematurely");
        exit(-1);
    }
    fputs(recvline, stdout);
}
}

main() {
int sock, adrlen, cnt;

sock = socket(AF_UNIX, SOCK_STREAM, 0);
if(sock < 0) {
    printf("client socket failure%d\n", errno);
    printf("client: ");
    exit(1);
}

myname.sa_family = AF_UNIX;
strcpy(myname.sa_data, "/tmp/billb");
adrlen = strlen(myname.sa_data) + sizeof(myname.sa_family);

if(connect(sock, &myname, adrlen) < 0) {
    printf("client connect failure %d\n", errno);
    perror("client: ");
    exit(1);
}

replyBack(stdin,sock);
exit(0);
}

And here is the server code:

#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

struct sockaddr myname;
char buf[80];

void echo(int sockfd) {
ssize_t n;
int write_err;
char buf[1000];
char * send_start_pos;
while(1) {
    bytes_in = read(sockfd, buf, 1000);
    if(bytes_in < 1) {
        if(errno == EINTR)
            continue;
        break;
    }
    bytes_remaining = bytes_in;
    send_开发者_如何学编程start_pos = buf;
    write_err = 0;

    while((bytes_remaining > 0) && !(write_err)) {
        bytes_out = write(sockfd, send_start_pos,           
                bytes_remaining);
        if(bytes_out < 0) {
            if(errno == EINTR)
                continue;
            write_err = 1;
            break;
        }
    bytes_remaining -= bytes_out;
    send_start_pos += bytes_out;
    }
    if(write_err)
        break;
}
}

main() {
int sock, new_sd, adrlen, cnt;

sock = socket(AF_UNIX, SOCK_STREAM, 0);
if(sock < 0) {
    printf("server socket failure %d\n", errno);
    perror("server: ");
    exit(1);
}

myname.sa_family = AF_UNIX;
strcpy(myname.sa_data, "/tmp/billb");
adrlen = strlen(myname.sa_data) + sizeof(myname.sa_family);

unlink("/tmp/billb"); /*defensive programming */
if(bind(sock, &myname, adrlen) < 0) {
    printf("server bind failure%d\n", errno);
    perror("server: ");
    exit(1);
}

if(listen(sock, 5) < 0) {
    printf("server listen failure %d\n", errno);
    perror("server: ");
    exit(1);
}

while(1) {
    if(new_sd = accept(sock, &myname, &adrlen) < 0) {
        printf("server accept failure %d\n", errno);
        perror("server: ");
        exit(1);
    }

    printf("Socket address in server %d is %s, %s\n", 
        getpid(), myname.sa_data, myname.sa_data);

    if(fork() == 0) {
        close(sock);
        echo(new_sd);
        exit(0);
    }
    close(new_sd);
}
}

The problem when I run the program is that the client is accepting the message but then does not actually send it to the server, so the server can never echo it back.

I know this is probably basic stuff so I appreciate your patience and time!


You're missing a truckload of includes. I took the time to compile your programs and this gem stood out of the crowd of warnings

warning: suggest parentheses around assignment used as truth value

At this line

if(new_sd = accept(sock, &myname, &adrlen) < 0) {

So basically you're checking if accept returns something lower than 0 and assigning that to new_sd. Change that line to

if((new_sd = accept(sock, &myname, &adrlen)) < 0) {

Bonus: Most of what @sarnold said in his answer is correct and in the long run will do you better than my answer.


adrlen = strlen(myname.sa_data) + sizeof(myname.sa_family);

This should be sizeof myname. The unix(7) sockets are actually defined to have a sockaddr_un like this:

       #define UNIX_PATH_MAX    108

       struct sockaddr_un {
           sa_family_t sun_family;               /* AF_UNIX */
           char        sun_path[UNIX_PATH_MAX];  /* pathname */
       };

You've given it 10+4 as the size. Which should almost work -- it doesn't count the terminating NUL byte at the end of the filename -- but even with the + 1 added, I'd feel better if you're passing in exactly the size of object you have. (Be careful of strlen(3) on strings. Almost always you need a + 1 in any expression that involves strlen(3). Funny.)

while(fgets(sendline,1000,stdin) != NULL) {
    write(sockfd,sendline,sizeof(sendline));

And here you're sending all 1000 bytes of your buffer, regardless of contents. The user might have entered one character, or one hundred, and you're sending all 1000 bytes of whatever is at that address.

Your echo method is a bit much; I'd recommend splitting the writing off into its own routine; Advanced Programming in the Unix Environment, 2nd edition (a superb book, well worth finding a copy if you expect to program Unix or Unix-like systems) has a small sweet routine I really enjoy:

ssize_t             /* Write "n" bytes to a descriptor  */
writen(int fd, const void *ptr, size_t n)
{
    size_t      nleft;
    ssize_t     nwritten;

    nleft = n;
    while (nleft > 0) {
        if ((nwritten = write(fd, ptr, nleft)) < 0) {
            if (nleft == n)
                return(-1); /* error, return -1 */
            else
                break;      /* error, return amount written so far */
        } else if (nwritten == 0) {
            break;
        }
        nleft -= nwritten;
        ptr   += nwritten;
    }
    return(n - nleft);      /* return >= 0 */
}

(See lib/writen.c in the source from the book's website.)

I haven't actually tried your code, so it's very possible none of these are the problem actually preventing your software from working :) if that's the case, leave a comment, and I'll take a closer look.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜