Enviado em 03/04/2015 - 16:53h
Boa tarde, estou tentado fazer um programa como telnet e o netcat, mas so a parte de envia e receber texto, mas nao estou conseguido fazer de jeito nenhum, o codigo:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
// blibioteca para socket Tcp
#include "SocketTcp.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
// e deu um progama que eu fis
// esta fucinado, pega perfeitamente com o nc ou telnet
#define PORT 20108
#define IP "127.0.0.1"
#define MAX_BUFFER 2000
#define PASS "EXIT"
int main(int argc, char const *argv[]){
// estrutura que representa um socket
Socket s;
char bufferOUT[100];
char bufferIN[MAX_BUFFER];
fd_set in;
struct timeval tv;
int sso, retval;
// configura o todas estruturas e verifica erros
if(setSocket(&s, IP, PORT) == false){
perror("setSocket");
return 1;
}
// chama o connect e verifica erros
if(getConecao(&s) == false){
perror("getConecao");
return 1;
}
FD_ZERO(&in);
FD_SET(0, &in);
FD_SET(getFd(s), &in);
sso = fcntl(getFd(s), F_GETFL, 0);
fcntl(getFd(s), F_SETFL, sso | O_NONBLOCK);
tv.tv_sec = 1;
tv.tv_usec = 0;
while(true){
int rr = select(getFd(s)+1, &in, NULL, NULL, &tv);
printf("Test 1\n");
if(rr < 0){
perror("select");
break;
}
if(rr == 0){
tv.tv_sec = 1;
tv.tv_usec = 0;
}
// Aqui esta fucionado
if(rr && FD_ISSET(getFd(s), &in) ){
printf("Test 3\n");
int r;
memset(bufferIN, 0, MAX_BUFFER);
r = read(getFd(s), bufferIN, MAX_BUFFER);
if(r < 0){
perror("read");
break;
}
if(r == 0){
printf("Connection closed by the remote end\n");
break;
}
bufferIN[r] = '\0';
printf("recebido: %s\n",bufferIN);
fflush(0);
}
// Nao esta entrando aqui
if(rr && FD_ISSET(0, &in) ){
printf("Test 2\n");
int r;
memset(bufferOUT, 0, 100);
r = read(0, bufferOUT, 100);
if(r < 0){
perror("read");
break;
}
printf("enviado: %s\n", bufferOUT);
write(getFd(s), bufferOUT, r);
}
}
closeSocket(&s);
printf("Em Fim!!!\n");
return 0;
}
O que é o THP na configuração de RAM do Linux e quando desabilitá-lo
Comparação entre os escalonadores BFQ e MQ-Deadline (acesso a disco) no Arch e Debian
Conciliando o uso da ZRAM e SWAP em disco na sua máquina
Servidor de Backup com Ubuntu Server 24.04 LTS, RAID e Duplicati (Dell PowerEdge T420)
Como unir duas coleções de ROMs preservando as versões traduzidas (sem duplicatas)
Como instalar o Telegram Desktop no Ubuntu 24.04
Overclocking Permanente para Drastic no Miyoo Mini Plus
Problemas de chaves (/usr/share/keyrings) no Debian
Converter os repositórios Debian para o novo formato com as chaves
eu preciso saber uma coisa sobre os games no linux (3)
eu preciso saber uma coisa sobre os games no linux (1)
Problema com audio apos upgrade (1)