Created
April 11, 2012 12:25
-
-
Save fjolnir/2359028 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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> | |
#include <dispatch/dispatch.h> | |
#include <readline/readline.h> | |
#include <readline/history.h> | |
bool running = true; | |
int sockfd, portNum; | |
struct sockaddr_in serverAddr; | |
struct hostent *server; | |
char buffer[1024]; | |
dispatch_source_t readSource; | |
dispatch_queue_t queue; | |
bool multiline = false; | |
static int returnKey(int count, int key) { | |
if(!multiline) | |
rl_done = 1; | |
printf("\n"); | |
} | |
static int toggleMultiline(int count, int key) { | |
if(multiline == false) { | |
multiline = true; | |
printf("\r~ "); | |
} else { | |
rl_done = 1; | |
printf("\n"); | |
multiline = false; | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
bzero((char *)&serverAddr, sizeof(serverAddr)); | |
if(argc < 3) { | |
fprintf(stderr, "Usage: client <server> <port>\n"); | |
return 1; | |
} | |
// Connect to the server | |
portNum = atoi(argv[2]); | |
sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
if(sockfd < 0) { | |
fprintf(stderr, "ERROR opening socket\n"); | |
return 1; | |
} | |
server = gethostbyname(argv[1]); | |
if(server == NULL) { | |
fprintf(stderr,"ERROR, no such host\n"); | |
return 1; | |
} | |
serverAddr.sin_family = AF_INET; | |
bcopy((char *)server->h_addr, (char *)&serverAddr.sin_addr.s_addr, server->h_length); | |
serverAddr.sin_port = htons(portNum); | |
if(connect(sockfd,(struct sockaddr *) &serverAddr,sizeof(serverAddr)) < 0) { | |
fprintf(stderr, "ERROR connecting\n"); | |
return 1; | |
} | |
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); | |
readSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, sockfd, 0, queue); | |
dispatch_source_set_event_handler(readSource, ^{ | |
char buf[1024]; | |
bzero(buf,sizeof(buf)); | |
int n = read(sockfd,buf, sizeof(buf)-1); | |
if(n < 0) | |
fprintf(stderr, "ERROR reading from socket\n"); | |
// Print the output without messing up the prompt | |
int savedPoint = rl_point; | |
char *savedLine = rl_copy_text(0, rl_end); | |
rl_set_prompt(""); | |
rl_replace_line("", 0); | |
rl_redisplay(); | |
fprintf(stderr, "\r%s", buf); | |
rl_set_prompt("> "); | |
rl_replace_line(savedLine, 0); | |
rl_point = savedPoint; | |
rl_redisplay(); | |
free(savedLine); | |
}); | |
dispatch_resume(readSource); | |
rl_bind_key('\n', returnKey); | |
rl_bind_key('\r', returnKey); | |
rl_bind_key('\t', toggleMultiline); | |
char *line; | |
while((line = readline("> ")) != NULL) { | |
if(strlen(line) > 0) { | |
if(strcmp(line, "q") == 0 || strcmp(line, "quit") == 0) | |
break; | |
ssize_t n = write(sockfd,line,strlen(line)); | |
if(n < 0) | |
fprintf(stderr, "ERROR writing to socket"); | |
add_history(line); | |
} | |
free(line); | |
} | |
close(sockfd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment