newbieatassembly
Technical User
Hi, I am trying to write a simple socket - send and reciive function to port 80.
Here is the code :
// Socket to test http port
// let us have some global variables
#include <stdio.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#define MAX_BUFFER_SIZE 16*1024
int len, bytes_sent;
char *msg = "GET\n";
char port[8] = "80";
struct sockaddr_in server;
struct hostent *h;
struct timeval to;
int socketopen = 1;
int main()
{
int sock_fd, tmp_connect;
fd_set rread, wwrite;
char recv_message[MAX_BUFFER_SIZE];
len = strlen(msg);
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if(sock_fd < 0) {perror("socket" return -1;}
// put in required resources
server.sin_family = AF_INET;
h = gethostbyname("localhost"
if(h == 0) return -1;
memcpy(&(server.sin_addr), h->h_addr, h->h_length);
server.sin_port = htons(atoi(port));
// now connect
tmp_connect = connect(sock_fd, (struct sockaddr *)
&server, sizeof(server));
if(tmp_connect < 0) { perror("connect"
return -1; }
do {
FD_ZERO(&rread); FD_ZERO(&wwrite);
FD_ISSET(sock_fd, &rread);
FD_ISSET(sock_fd, &wwrite);
memset((char *)&to, 0, sizeof(to));
to.tv_sec = 5;
int sw = select(sock_fd + 1, (fd_set *)0,
&wwrite, (fd_set *)0, &to);
if(sw < 0) { perror("select" return -1;}
if(sw) {
// data can be written now
send(sock_fd, msg, len, 0);
}
int sr = select(sock_fd + 1, &rread,
(fd_set *)0,(fd_set *)0, &to);
if(sr < 0) { perror("select read"
return -1; }
if(sr) {
// data can be read now
int rval = read(sock_fd,
recv_message, 16000);
if(rval < 0) { perror("read"
return -1; }
printf("\n%s\n", recv_message);
socketopen = 0;
}
}while(socketopen == 1);
close(sock_fd) ;
return 0;
}
It seems to sending nothing, strace seems to have a sendto(something,something MSB_OOB);
I think something is wrong with the code. I know this code looks like C, so if anyone is offended by this post I am sorry.
Here is the code :
// Socket to test http port
// let us have some global variables
#include <stdio.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#define MAX_BUFFER_SIZE 16*1024
int len, bytes_sent;
char *msg = "GET\n";
char port[8] = "80";
struct sockaddr_in server;
struct hostent *h;
struct timeval to;
int socketopen = 1;
int main()
{
int sock_fd, tmp_connect;
fd_set rread, wwrite;
char recv_message[MAX_BUFFER_SIZE];
len = strlen(msg);
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if(sock_fd < 0) {perror("socket" return -1;}
// put in required resources
server.sin_family = AF_INET;
h = gethostbyname("localhost"
if(h == 0) return -1;
memcpy(&(server.sin_addr), h->h_addr, h->h_length);
server.sin_port = htons(atoi(port));
// now connect
tmp_connect = connect(sock_fd, (struct sockaddr *)
&server, sizeof(server));
if(tmp_connect < 0) { perror("connect"
return -1; }
do {
FD_ZERO(&rread); FD_ZERO(&wwrite);
FD_ISSET(sock_fd, &rread);
FD_ISSET(sock_fd, &wwrite);
memset((char *)&to, 0, sizeof(to));
to.tv_sec = 5;
int sw = select(sock_fd + 1, (fd_set *)0,
&wwrite, (fd_set *)0, &to);
if(sw < 0) { perror("select" return -1;}
if(sw) {
// data can be written now
send(sock_fd, msg, len, 0);
}
int sr = select(sock_fd + 1, &rread,
(fd_set *)0,(fd_set *)0, &to);
if(sr < 0) { perror("select read"
return -1; }
if(sr) {
// data can be read now
int rval = read(sock_fd,
recv_message, 16000);
if(rval < 0) { perror("read"
return -1; }
printf("\n%s\n", recv_message);
socketopen = 0;
}
}while(socketopen == 1);
close(sock_fd) ;
return 0;
}
It seems to sending nothing, strace seems to have a sendto(something,something MSB_OOB);
I think something is wrong with the code. I know this code looks like C, so if anyone is offended by this post I am sorry.