Skip to content
Permalink
dccea4b510
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
63 lines (58 sloc) 1.44 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/select.h>
#include <assert.h>
void checkError(int status,int line)
{
if (status < 0) {
printf("socket error(%d)-%d: [%s]\n",getpid(),line,strerror(errno));
exit(-1);
}
}
int main(int argc, char ** argv) {
int sid = socket(PF_INET,SOCK_STREAM,0);
struct sockaddr_in srv;
struct hostent *server = gethostbyname("localhost");
if (server==NULL) {
printf("Couldn't find a host named: %s\n",argv[1]);
return 2;
}
srv.sin_family = AF_INET;
srv.sin_port = htons(8025);
memcpy(&srv.sin_addr,server->h_addr_list[0],server->h_length);
int status = connect(sid,(struct sockaddr*)&srv,sizeof(srv));
checkError(status,__LINE__);
fd_set readfds;
char ch;
while (1) {
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(sid, &readfds);
int ready = select(sid+1, &readfds, NULL,NULL,NULL);
if (ready) {
if (FD_ISSET(0,&readfds)) {
int rc = read(0,&ch,1);
if (!rc) exit(1);
send(sid, &ch, 1,0);
} else if (FD_ISSET(sid,&readfds)) { // if there are data in sid
int rc = read(sid,&ch,1);
if (rc > 0) {
printf("%c", ch);
}
else {
fprintf(stderr, "exiting remote shell\n");
close(sid);
exit(-2);
}
}
}
}
}