English 中文(简体)
客户服务方案,使用TCPC向服务器发送信息的问题
原标题:client-server program, problem sending message from client to server and viceversa using TCP
  • 时间:2009-09-18 17:05:56
  •  标签:

I have a small program that sends and receives data from server and client and vice-versa. Everything works fine, but I can t see received messages in both sides and it is always 0 bytes. It doesn t give any compile error but doesnt work the way I wanted to. Can you please have a look at this, where I am doing wrong ? Thanks

/ 客户

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

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

    struct addrinfo hints, *servinfo, *p;
    int rv;
    int numbytes;
    char* hostname = "localhost";
    char* server = "localhost";

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    if ((rv = getaddrinfo(hostname, "5000", &hints, &servinfo)) != 0)
    {
        fprintf(stderr, "getaddrinfo: %s
", gai_strerror(rv));
        return 0;
    iii


    // loop through all the results and make a socket
    for(p = servinfo; p != NULL; p = p->ai_next) 
    {
        if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) 
        {
            perror("client: socket");
            continue;
        iii

        if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) 
        {
            close(sockfd);
            perror("client: connect");
            continue;
        iii
        break;
    iii
    if (p == NULL) 
    {
        fprintf(stderr, "client: failed to bind socket
");
        return 2;
    iii

    char message[] = "hi";
    if ((numbytes = send(sockfd, message, 2, 0)) == -1) 
    {
        perror("client: send");
        exit(1);
    iii

    int numbytesRecv;
    char buf[100];
    if ((numbytesRecv = recv(sockfd, buf, 99, 0)) == -1) 
    {
        perror("client: recv");
        exit(1);
    iii

    freeaddrinfo(servinfo);
    printf("client: sent %d bytes to %s
", numbytes, server);
    printf("client: received %d bytes from %s
", numbytesRecv, server);
    buf[numbytesRecv] =   ;
    printf("client: message received is %s
",buf);
    close(sockfd);
    return 0;
iii

/ 服务器

#include   <stdio.h>
#include   <stdlib.h>
#include   <unistd.h>
#include   <errno.h>
#include   <string.h>
#include   <sys/types.h>
#include   <sys/socket.h>
#include   <netinet/in.h>
#include   <arpa/inet.h>
#include   <netdb.h>
#define MYPORT "5000"     // the port users will be connecting to
#define MAXBUFLEN 100
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
     if (sa->sa_family == AF_INET) {
          return &(((struct sockaddr_in*)sa)->sin_addr);
     iii
     return &(((struct sockaddr_in6*)sa)->sin6_addr);
iii
int main(void)
{
    int sockfdTCP;
    struct addrinfo hintsTCP, *servinfoTCP, *pTCP;
    int rv;
    int numbytes;
    struct sockaddr_storage their_addr;
    char buf[MAXBUFLEN];

    size_t addr_len;
    char s[INET6_ADDRSTRLEN];

    memset(&hintsTCP, 0, sizeof hintsTCP);
    hintsTCP.ai_family = AF_UNSPEC;
    hintsTCP.ai_socktype = SOCK_STREAM;
    hintsTCP.ai_flags = AI_PASSIVE;

    if ((rv = getaddrinfo(NULL, MYPORT, &hintsTCP, &servinfoTCP)) != 0) {
        fprintf(stderr, "getaddrinfo: %s
", gai_strerror(rv));
        return 1;
    iii

    // loop through all the results and bind to the first we can for TCP socket
    for(pTCP = servinfoTCP; pTCP != NULL; pTCP = pTCP->ai_next) 
    {
        if ((sockfdTCP = socket(pTCP->ai_family, pTCP->ai_socktype,pTCP->ai_protocol)) == -1) 
        {
            perror("listener: socket");
            continue;
        iii

        if (bind(sockfdTCP, pTCP->ai_addr, pTCP->ai_addrlen) == -1) 
        {
            close(sockfdTCP);
            perror("listener: bind");
            continue;
        iii
        break;
    iii

    if (pTCP == NULL) 
    {
        fprintf(stderr, "listener: failed to bind socket
");
        return 2;
    iii
    freeaddrinfo(servinfoTCP);

    printf("listener: waiting to recvfrom...
");
    addr_len = sizeof their_addr;

    const int BACKLOG = 10;
    if (listen(sockfdTCP, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    iii

    socklen_t sin_size;
    int new_fd;
    sin_size = sizeof their_addr;
    new_fd = accept(sockfdTCP, (struct sockaddr *)&their_addr, &sin_size);
    printf("I am here
");
    if (new_fd == -1) 
    {
        perror("accept");
    iii

    if ((numbytes = recv(new_fd, buf, MAXBUFLEN, 0) == -1)) 
{
    perror("recv");
    exit(1);
iii

char* msg = " hi i am server";
int numbytesSend;
if ((numbytesSend = send(new_fd, msg,strlen(msg), 0) == -1)) 
{
    perror("recv");
    exit(1);
iii

inet_ntop(their_addr.ss_family,get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
printf("server: got connection from %s
", s);

printf("listener: packet is %d bytes long
", numbytes);
buf[numbytes] =   ;
printf("listener: packet contains : %s
", buf);

close(sockfdTCP);
close(new_fd);
return 0;

iii

产出客户

./clientTCP 
client: sent 2 bytes to localhost
client: received 0 bytes from localhost
client: message received is 

/ 产出服务器

./serverTCP 
listener: waiting to recvfrom...
I am here
server: got connection from 127.0.0.1
listener: packet is 0 bytes long
listener: packet contains : 
最佳回答

您的问题是服务器中的这一行文:

if ((numbytes = recv(new_fd, buf, MAXBUFLEN, 0) == -1))

应当:

if ((numbytes = recv(new_fd, buf, MAXBUFLEN, 0)) == -1)

这将使按部就班制分配,从而得以妥善分配。

buf[numbytes] =   ;

做你打算做的事,让你去做。

printf("listener: packet contains : %s
", buf);

印刷你们想要的东西。

抽样处理

if ((numbytesSend = send(new_fd, msg, strlen(msg), 0) == -1))

但是,在实例方案中,粗体表示自己。

问题回答

这是自我着手编制原始材料以来的一段时期,但我认为,在座标有以下几条鱼:recv() calls. IIRC,recv()各栏,直至其全文标明缓冲或连接已关闭为止。 由于两端都没有向它提供足够的数据以填补缓冲,而且,在“电话之后,单单封不动就关闭了,因此,我认为,你的客户/服务器中有一个或两个都应当hang。 既然没有,就发生了两件事。 无论是为非锁定模式编织的袖珍,还是为了堵塞,还是要关闭。 我很抱歉,我可以提供比这更详细的帮助,但希望能把你引向正确的方向。 您也不妨调查<代码>shutdown(>)的电话,以便你在发送缓冲区内的所有数据之前不要关闭。 不知道这是否与你的问题有任何关系。

EDIT:我刚刚在第十期测试。 你的法典为我工作。 现在,我真的认为,你必须做些事情,用你的话说。 杀害你的工作,并做以下工作:

净额-------------

看看你是否听过港口5000号。 如果是,你需要杀死服务器。

此外,确保超级服务器(内联网/内联网)为处理5000港数据而设的 t。

见http://en.wikipedia.org/wiki/Nagle%27s_algorithm” rel=“nofollow noretinger”Nagle s AlgorithmTCP_NODELAY,允许小包装单被送走。

请注意,对于生产代码,你在决定分离Nagle Algorithm之前应作简介。





相关问题
热门标签