Last active
December 18, 2015 13:39
-
-
Save yanhan/5791613 to your computer and use it in GitHub Desktop.
My answer for stackoverflow question http://stackoverflow.com/questions/17131565/python-writes-to-c-programs-stdin
This file contains 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 <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#define BUFSZ 256 | |
int main(int argc, char *argv[]) | |
{ | |
int sockfd, ret, exitCode = 0; | |
char buffer[BUFSZ]; | |
memset(buffer, 0, BUFSZ); | |
fgets(buffer, sizeof(buffer), stdin); | |
char *hostname = argv[1]; | |
int port = atoi(argv[2]); | |
// echo out the stuff just to see | |
printf("hostname = %s\n", hostname); | |
printf("port = %d\n", port); | |
printf("msg = %s\n", buffer); | |
// creates the socket | |
sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sockfd == -1) { | |
fprintf(stderr, "client: socket() failed\n"); | |
exit(1); | |
} | |
struct sockaddr_in sockAddr; | |
memset(&sockAddr, 0, sizeof(struct sockaddr_in)); | |
sockAddr.sin_family = AF_INET; | |
sockAddr.sin_port = htons(port); | |
sockAddr.sin_addr.s_addr = inet_addr(hostname); | |
// connect to server | |
ret = connect(sockfd, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr)); | |
if (ret == -1) { | |
fprintf(stderr, "client: connect failed\n"); | |
exitCode = 1; | |
goto done; | |
} | |
// send buffer | |
ssize_t charsSent = send(sockfd, buffer, strlen(buffer)+1, 0); | |
printf("client: chars sent: %d\n", (int)charsSent); | |
/* | |
// this seems to work as well | |
write(sockfd, buffer, strlen(buffer)+1); | |
*/ | |
done: | |
close(sockfd); | |
return exitCode; | |
} |
This file contains 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 <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#define PORTNUM 8124 | |
#define BUFSZ 256 | |
int main(int argc, char *argv[]) | |
{ | |
char buf[BUFSZ]; | |
int ret, exitCode = 1; | |
// create socket | |
int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
int recvFd; | |
if (sockfd == -1) { | |
fprintf(stderr, "server: Error creating socket\n"); | |
exit(1); | |
} | |
struct sockaddr_in sockAddr; | |
memset(&sockAddr, 0, sizeof(struct sockaddr_in)); | |
sockAddr.sin_family = AF_INET; | |
sockAddr.sin_port = htons(PORTNUM); | |
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); | |
// bind socket | |
ret = bind(sockfd, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr)); | |
if (ret == -1) { | |
fprintf(stderr, "server: bind failed\n"); | |
exitCode = 1; | |
goto done; | |
} | |
// listen for connection. max of 1 connection | |
ret = listen(sockfd, 1); | |
if (ret == -1) { | |
fprintf(stderr, "server: listen failed\n"); | |
exitCode = 1; | |
goto done; | |
} | |
struct sockaddr_in dest; | |
socklen_t sockLen; | |
ssize_t bytesReceived; | |
while (1) { | |
// accept connection from client | |
recvFd = accept(sockfd, (struct sockaddr *)&dest, &sockLen); | |
if (recvFd == -1) { | |
fprintf(stderr, "server: accept failed\n"); | |
exitCode = 1; | |
break; | |
} | |
// receive message from client | |
bytesReceived = recv(recvFd, buf, BUFSZ, 0); | |
if (bytesReceived == -1) { | |
fprintf(stderr, "server: recv failed\n"); | |
exitCode = 1; | |
break; | |
} else { | |
printf("server: received %s", buf); | |
fflush(stdout); | |
} | |
} | |
done: | |
close(sockfd); | |
return exitCode; | |
} |
This file contains 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
from subprocess import Popen, PIPE | |
def run(): | |
# this should work if you change to | |
# host_name = raw_input | |
# port = raw_input | |
host_name = '127.0.0.1' | |
port = '8124' | |
p = Popen(['./client', host_name, port], stdin=PIPE, close_fds=True) | |
myInput = raw_input() | |
p.stdin.write(myInput) | |
p.communicate() | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment