2014-09-02 136 views
-1

这是我的C客户端代码。不知何故,它不工作。它在我尝试传递参数时起作用。
我希望程序要求用户给hostname然后它会要求portname,然后发送消息:C socket程序错误

Enter hostname: localhost
Enter portname: 56456
Enter message : Hi user
Enter message : What's up
Enter message : How are you

而且一旦给了它不应该要求再次(直到重新启动该程序的主机和端口)。我试着用do while循环,但它不工作。 在服务器上会显示已发送的邮件

这里是我的代码:

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

void error(const char *msg) 
{ 
    perror(msg); 
    exit(0); 
} 

//int main(int argc, char *argv[]) 
int main() 
{ 
    char *argv[256]; 
    int argc; 
    int sockfd, portno, n; 
    struct sockaddr_in serv_addr; 
    struct hostent *server; 
    printf("\n\nEnter Hostname\n\n"); 
    fgets(argv[0],256,stdin); 
    char buffer[256]; 
    if (argc < 3) { 
     fprintf(stderr,"usage %s hostname port\n", argv[0]); 
     exit(0); 
    } 
    portno = atoi(argv[2]); 
    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    if (sockfd < 0) 
     error("ERROR opening socket"); 
    server = gethostbyname(argv[1]); 
    if (server == NULL) { 
     fprintf(stderr,"ERROR, no such host\n"); 
     exit(0); 
    } 
    bzero((char *) &serv_addr, sizeof(serv_addr)); 
    serv_addr.sin_family = AF_INET; 
    bcopy((char *)server->h_addr, 
     (char *)&serv_addr.sin_addr.s_addr, 
     server->h_length); 
    serv_addr.sin_port = htons(portno); 
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
     error("ERROR connecting"); 


    printf("Please enter the message: "); 
    bzero(buffer,256); 
    //buffer = tempFunc(); 
    fgets(buffer,255,stdin); 
    printf("\n\nHere Goes the output\n%s",buffer); 
    n = write(sockfd,buffer,strlen(buffer)); 
    if (n < 0) 
     error("ERROR writing to socket"); 
    bzero(buffer,256); 
    n = read(sockfd,buffer,255); 
    if (n < 0) 
     error("ERROR reading from socket"); 
    printf("%s\n",buffer); 
    close(sockfd); 
    return 0; 

} 
+0

代码不匹配给定的输出。 'do while'循环在哪里? – Coconop 2014-09-02 12:12:07

+0

我删除了这个,如果你想我会添加它 – 2014-09-02 12:13:18

+0

你尝试使用多路复用器,如选择,轮询或epoll。你应该阅读一下,因为这个任务很简单。 – 2014-09-02 12:16:12

回答

2

首先,不使用char * argv[256]

char buffer[256]; 
printf("\n\nEnter Hostname\n\n"); 
fgets(buffer,256,stdin); 

然后检查Removing trailing newline character from fgets() input处理fgets

对于一个无限循环,不做

int a=2; // Useless declaration 
do 
{ 
    // Your code 
}while(a=2) // I guess you wanted (a == 2) 

使用:

while(1) 
{ 
    // Your code 
} 

或者

for(;;) 
{ 
    // Your code 
} 

看来你需要的训练一点点,尝试一些教程,寻找C的良好实践,在编译时启用警告标志并学习使用调试器,如gdb

P.S:

2

让我们首先解决一些基本的东西。
你的argv是一个指针数组,指向内存中的任意位置,在这里你的程序可能会崩溃。
接下来的事情是,当您在阅读fgets的输入时,您也在阅读\n。所以localhost\n不是一个有效的主机名。用二进制零覆盖最后一个字符,删除\n

int main() 
{ 

    char hostname[256]; 
    char port[16]; 
    char buffer[256]; 
    int sockfd, portno, n; 
    struct sockaddr_in serv_addr; 
    struct hostent *server; 
    printf("\n\nEnter Hostname\n\n"); 
    fgets(hostname, 256,stdin); 

    hostname[ strlen(hostname) - 1 ] = '\0'; 
    fgets(port, 16, stdin); 
    port[ strlen(port) - 1] = '\0'; 

    portno = atoi(port); 
    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    if (sockfd < 0) 
     error("ERROR opening socket"); 
    server = gethostbyname(hostname); 
    if (server == NULL) { 
     fprintf(stderr,"ERROR, no such host\n"); 
    exit(0); 
    //... 
} 
+0

它正在工作,我添加了while while while while well – 2014-09-02 12:54:57

-1

这是循环的代码。由于@Coconop

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

void error(const char *msg) 
{ 
    perror(msg); 
    exit(0); 
} 

int main() 
{ 

    char hostname[256]; 
    char port[16]; 
    char buffer[256]; 
    int sockfd, portno, n; 
    struct sockaddr_in serv_addr; 
    struct hostent *server; 
    printf("\n\nEnter Hostname\n\n"); 
    fgets(hostname, 256,stdin); 

    hostname[ strlen(hostname) - 1 ] = '\0'; 
    fgets(port, 16, stdin); 
    port[ strlen(port) - 1] = '\0'; 

    portno = atoi(port); 
    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    if (sockfd < 0) 
     error("ERROR opening socket"); 
    server = gethostbyname(hostname); 
    if (server == NULL) { 
     fprintf(stderr,"ERROR, no such host\n"); 
    exit(0); 
    } 

bzero((char *) &serv_addr, sizeof(serv_addr)); 
    serv_addr.sin_family = AF_INET; 
    bcopy((char *)server->h_addr, 
     (char *)&serv_addr.sin_addr.s_addr, 
     server->h_length); 
    serv_addr.sin_port = htons(portno); 
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
     error("ERROR connecting"); 
int a=2; 
do { 
    printf("Please enter the message: "); 
    bzero(buffer,256); 
    //buffer = tempFunc(); 
    fgets(buffer,255,stdin); 
    printf("\n\nHere Goes the output\n%s",buffer); 
    n = write(sockfd,buffer,strlen(buffer)); 
}while(a=2); 
    if (n < 0) 
     error("ERROR writing to socket"); 
    bzero(buffer,256); 
    n = read(sockfd,buffer,255); 
    if (n < 0) 
     error("ERROR reading from socket"); 
    printf("%s\n",buffer); 
    close(sockfd); 
    return 0; 

} 

+0

如果它解决了你的问题,你应该接受一个建议的答案并删除这个。 另请注意,'do while'循环无用,因为'a'在循环内没有修改 – Coconop 2014-09-02 13:00:00

+0

@Coconop我想要一个无限循环,因为这个原因我做了这个并且所需的输出是正确的 – 2014-09-02 13:04:51

+1

无论如何,你不应该发布你的如果它是基于某人的回答,那么只有当它带来新的东西时才有答案。要增加你的问题的精度,你可以编辑你的帖子。 012vUpvote帮助您解决问题的所有答案,并接受更有用的答案。 – Coconop 2014-09-02 13:16:32

0

对于命令行参数,你不应该再申报argcargv!试试这个吧 -

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

void error(const char *msg) 
{ 
     perror(msg); 
     exit(0); 
} 

int main(int argc, char *argv[]) 
{ 
     int sockfd, portno, n; 
     struct sockaddr_in serv_addr; 
     struct hostent *server; 
     char buffer[256]; 
     if (argc < 3) { 
       fprintf(stderr,"usage %s hostname port\n", argv[0]); 
       exit(0); 
     } 
     portno = atoi(argv[2]); 
     sockfd = socket(AF_INET, SOCK_STREAM, 0); 
     if (sockfd < 0) 
       error("ERROR opening socket"); 
     server = gethostbyname(argv[1]); 
     if (server == NULL) { 
       fprintf(stderr,"ERROR, no such host\n"); 
       exit(0); 
     } 
     bzero((char *) &serv_addr, sizeof(serv_addr)); 
     serv_addr.sin_family = AF_INET; 
     bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr, 
         server->h_length); 
     serv_addr.sin_port = htons(portno); 
     if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
       error("ERROR connecting"); 


     printf("Please enter the message: "); 
     bzero(buffer,256); 
     //buffer = tempFunc(); 
     fgets(buffer,255,stdin); 
     printf("\n\nHere Goes the output\n%s",buffer); 
     n = write(sockfd,buffer,strlen(buffer)); 
     if (n < 0) 
       error("ERROR writing to socket"); 
     bzero(buffer,256); 
     n = read(sockfd,buffer,255); 
     if (n < 0) 
       error("ERROR reading from socket"); 
     printf("%s\n",buffer); 
     close(sockfd); 
     return 0; 

} 

并且在你想要的地方添加do while while!