这是我的C客户端代码。不知怎的,它不起作用。当我尝试通过论证时,它起作用了
我希望程序要求用户提供hostname
然后它会要求portname
然后要发送的消息:
输入主机名:localhost
输入端口名称:56456
输入消息:嗨用户
输入消息:内容是什么 输入消息:你好吗
一旦给出它的主机和端口不应该再次要求(直到重启程序)。我尝试使用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 :(得分: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);
//...
}
答案 1 :(得分: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 :(得分:0)
对于命令行参数,您不应再次声明argc
和argv
!试试这个 -
#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循环!
答案 3 :(得分:-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;
}