Telnet klient i C, lavet til Linux
Jeg har programmeret en Telnet klient, men alt hvad denreturnerer er nogle tegn. Jeg synes jeg har prøvet alt
Er der nogen der kan hjælpe?
*********************************************
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
struct sockaddr_in address;
int result, nread, len, sockfd;
fd_set inputs, fdTest;
char ch;
struct timeval timeout;
FD_ZERO(&inputs);
FD_SET(0, &inputs);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("208.231.29.131");
address.sin_port = htons(23);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
FD_SET(sockfd, &inputs);
if(result == -1) {
perror("oops: client");
exit(1);
}
while(1){
fdTest = inputs;
result = select(FD_SETSIZE, &fdTest, (fd_set *)NULL, (fd_set *)NULL, (struct timeval *) 0);
if(result == -1) {
perror("select");
exit(1);
}
if(FD_ISSET(0, &fdTest)) {
ioctl(0, FIONREAD, &nread);
if(nread == 0) {
close(sockfd);
exit(0);
}
ch = getchar();
// read(0, &ch, nread);
write(sockfd, &ch, 1);
}
else {
ioctl(sockfd, FIONREAD, &nread);
if(nread == 0) {
close(sockfd);
exit(0);
}
read(sockfd, &ch, 1);
printf("%c", ch);
}
}
}
