I have written a program sending a simple text to server. And I just print ftp >
to my terminal. But the program does not print ftp >
to terminal.
My OS is OS X Mountain Lion. Code snippet below for reference.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#define MAXLINE 1024
#define PORTNUM 3600
int main(int argc, char** argv)
{
int sockfd;
char buf[MAXLINE];
char* prompt = "ftp> ";
struct sockaddr_in serveraddr;
socklen_t addrlen;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serveraddr, 0x00, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serveraddr.sin_port = htons(PORTNUM);
addrlen = sizeof(serveraddr);
if (connect(sockfd, (struct sockaddr_in*)&serveraddr, addrlen)== -1)
{
perror("connect error :");
return 0;
}
while(1)
{
//write(STDOUT_FILENO, prompt, strlen(prompt));
printf("ftp> ");
read(STDIN_FILENO, buf, MAXLINE);
if( write(sockfd, buf, MAXLINE) <= 0)
{
perror("write error");
return 0;
}
}
return 0;
}
printf()
uses buffered I/O. The key is to call fflush(stdout);
after your printf()
, which is triggered typically by newlines ( '\\n'
) in the format string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.