cannot receive raw ethernet frame packets I just sent - sockets
I referenced C code on this website: https://gist.github.com/austinmarton/2862515
Here is a image to explain:
I edit it at two parts, one pat is for ethertype(change 0x0800 to a custom-protocol 0x1234)
and another part is deleting code for IP header processing (because original code is based on IP, but I need a raw ethernet frame).
I used wireshark to detect packets, and I can receive the packets I sent(in left of image),and I can see send.out exactly sending packets(bottom right of image). But recv.out cannot receive packet !?(upper right of image).
However, if I use 0x0800 for protocol, recv.out can receive packets from outside, but still cannot receive the packets I sent.
Is there any mistake for setting socket
Here is my code:
send.c
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#define MY_DEST_MAC0 0xbc
#define MY_DEST_MAC1 0xee
#define MY_DEST_MAC2 0x7b
#define MY_DEST_MAC3 0x75
#define MY_DEST_MAC4 0x56
#define MY_DEST_MAC5 0x2a
#define DEFAULT_IF "eth0"
#define BUF_SIZ 1024
int main(int argc, char *argv[])
{
int sockfd;
struct ifreq if_idx;
struct ifreq if_mac;
int tx_len = 0;
char sendbuf[BUF_SIZ];
struct ether_header *eh = (struct ether_header *) sendbuf; /*structure*/
struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header));
struct sockaddr_ll socket_address;
char ifName[IFNAMSIZ];
unsigned short proto = 0x1234;
/* Get interface name *//*eth0*/
if (argc > 1)
strcpy(ifName, argv[1]);
else
strcpy(ifName, DEFAULT_IF);
/* Open RAW socket to send on *//*IPv4*/
if ((sockfd = socket(AF_PACKET, SOCK_RAW, htons(proto))) == -1) {
perror("socket");
}
/* Get the index of the interface to send on *//*0*/
memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, ifName, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0)/*save INDEX info into if_idx*/
perror("SIOCGIFINDEX");
/* Get the MAC address of the interface to send on *//*local*//*save MAC info into if_mac*/
memset(&if_mac, 0, sizeof(struct ifreq));
strncpy(if_mac.ifr_name, ifName, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0)
perror("SIOCGIFHWADDR");
/* Construct the Ethernet header */
memset(sendbuf, 0, BUF_SIZ);
/* Ethernet header */
eh->ether_shost[0] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[0];
eh->ether_shost[1] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[1];
eh->ether_shost[2] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[2];
eh->ether_shost[3] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[3];
eh->ether_shost[4] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[4];
eh->ether_shost[5] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[5];
eh->ether_dhost[0] = MY_DEST_MAC0;
eh->ether_dhost[1] = MY_DEST_MAC1;
eh->ether_dhost[2] = MY_DEST_MAC2;
eh->ether_dhost[3] = MY_DEST_MAC3;
eh->ether_dhost[4] = MY_DEST_MAC4;
eh->ether_dhost[5] = MY_DEST_MAC5;
/* Ethertype field */
eh->ether_type = htons(proto);
tx_len += sizeof(struct ether_header);
/* Packet data */
sendbuf[tx_len++] = "h";
sendbuf[tx_len++] = "e";
sendbuf[tx_len++] = "l";
sendbuf[tx_len++] = "l";
sendbuf[tx_len++] = "o";
/* Index of the network device */
socket_address.sll_ifindex = if_idx.ifr_ifindex;
/* Address length*/
socket_address.sll_halen = ETH_ALEN;
/* Destination MAC */
socket_address.sll_addr[0] = MY_DEST_MAC0;
socket_address.sll_addr[1] = MY_DEST_MAC1;
socket_address.sll_addr[2] = MY_DEST_MAC2;
socket_address.sll_addr[3] = MY_DEST_MAC3;
socket_address.sll_addr[4] = MY_DEST_MAC4;
socket_address.sll_addr[5] = MY_DEST_MAC5;
/* Send packet */
int cnt=0;
while(cnt<5){
if (sendto(sockfd, sendbuf, tx_len, 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0)
printf("Send failed\n");
else
printf("success!\n");
cnt++;
}
return 0;
}
recv.c
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#define DEST_MAC0 0xbc
#define DEST_MAC1 0xee
#define DEST_MAC2 0x7b
#define DEST_MAC3 0x75
#define DEST_MAC4 0x56
#define DEST_MAC5 0x2a
#define ETHER_TYPE 0x1234
#define DEFAULT_IF "eth0"
#define BUF_SIZ 1024
int main(int argc, char *argv[])
{
char sender[INET6_ADDRSTRLEN];
int sockfd, ret, i;
int sockopt;
ssize_t numbytes;
struct ifreq ifopts; /* set promiscuous mode */
struct sockaddr_storage their_addr;
uint8_t buf[BUF_SIZ];
char ifName[IFNAMSIZ];
/* Get interface name *//*eth0*/
if (argc > 1)
strcpy(ifName, argv[1]);
else
strcpy(ifName, DEFAULT_IF);
/* Header structures */
struct ether_header *eh = (struct ether_header *) buf;
/* Open PF_PACKET socket, listening for EtherType ETHER_TYPE *//*0x1234*/
if ((sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETHER_TYPE))) == -1) {
perror("listener: socket");
return -1;
}
/* Set interface to promiscuous mode - do we need to do this every time? *//*cpy ifname into ifr_name*/
strncpy(ifopts.ifr_name, ifName, IFNAMSIZ-1);
ioctl(sockfd, SIOCGIFFLAGS, &ifopts); /*set promisc mode*/
ifopts.ifr_flags |= IFF_PROMISC;
ioctl(sockfd, SIOCSIFFLAGS, &ifopts);
/* Allow the socket to be reused - incase connection is closed prematurely */
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof sockopt) == -1) {
perror("setsockopt");
close(sockfd);
exit(EXIT_FAILURE);
}
/* Bind to device */
if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, ifName, IFNAMSIZ-1) == -1) {
perror("SO_BINDTODEVICE");
close(sockfd);
exit(EXIT_FAILURE);
}
repeat: printf("listener: Waiting to recvfrom...\n");
numbytes = recvfrom(sockfd, buf, BUF_SIZ, 0, NULL, NULL);
printf("listener: got packet %lu bytes\n", numbytes);
/* Check the packet is for me */
if (eh->ether_dhost[0] == DEST_MAC0 &&
eh->ether_dhost[1] == DEST_MAC1 &&
eh->ether_dhost[2] == DEST_MAC2 &&
eh->ether_dhost[3] == DEST_MAC3 &&
eh->ether_dhost[4] == DEST_MAC4 &&
eh->ether_dhost[5] == DEST_MAC5) {
printf("Correct destination MAC address\n");
} else {
printf("Wrong destination MAC: %x:%x:%x:%x:%x:%x\n",
eh->ether_dhost[0],
eh->ether_dhost[1],
eh->ether_dhost[2],
eh->ether_dhost[3],
eh->ether_dhost[4],
eh->ether_dhost[5]);
ret = -1;
goto done;
}
/* Print packet */
printf("\tData:");
for (i=0; i<numbytes; i++) printf("%02x:", buf[i]);
printf("\n");
done: goto repeat;
close(sockfd);
return ret;
}
you can get All frames from your target with below changes in your receive code :
please replace line :
if ((sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETHER_TYPE))) == -1) {
with this line :
if ((sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
Because in your original code you changed normal ethernet header type then OS can not detect its process listener(your program) and get it to you but when you set this line, OS can get you all results so you can get your special answers.
I'm starting to do socket programming so someone should confirm this.
I'm pretty sure the interface will drop the packet because the source and destination mac are the same... try using another PC and change the destination mac on each side to confirm this (virtual machines work as well)
I have also used a similar code to transfer Ethernet frames. This type of socket does not work locally. As #Goncalo suggested use a different PC or if you have two NICs on your PC you should use them. Here is the Code I used to receive Frames.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <sys/ioctl.h>
union ethframe
{
struct
{
struct ethhdr header;
unsigned char data[ETH_DATA_LEN];
} field;
unsigned char buffer[ETH_FRAME_LEN];
};
int main(int argc, char **argv) {
char *iface = "eth1";
unsigned char dest[ETH_ALEN]
= { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
unsigned short proto = 0x1234;
int recv_result,i;
char buff[ETH_FRAME_LEN];
unsigned short data_len;
int s;
if ((s = socket(AF_PACKET, SOCK_RAW, htons(proto))) < 0) {
printf("Error: could not open socket\n");
return -1;
}
struct ifreq buffer;
int ifindex;
memset(&buffer, 0x00, sizeof(buffer));
strncpy(buffer.ifr_name, iface, IFNAMSIZ);
if (ioctl(s, SIOCGIFINDEX, &buffer) < 0) {
printf("Error: could not get interface index\n");
close(s);
return -1;
}
ifindex = buffer.ifr_ifindex;
unsigned char source[ETH_ALEN];
if (ioctl(s, SIOCGIFHWADDR, &buffer) < 0) {
printf("Error: could not get interface address\n");
close(s);
return -1;
}
memcpy((void*)source, (void*)(buffer.ifr_hwaddr.sa_data),
ETH_ALEN);
struct sockaddr_ll saddrll;
memset((void*)&saddrll, 0, sizeof(saddrll));
saddrll.sll_family = PF_PACKET;
saddrll.sll_ifindex = ifindex;
saddrll.sll_halen = ETH_ALEN;
memcpy((void*)(saddrll.sll_addr), (void*)dest, ETH_ALEN);
socklen_t sll_len = (socklen_t)sizeof(saddrll);
if (recv_result = recvfrom(s, buff, ETH_FRAME_LEN, 0,
(struct sockaddr *)&saddrll, &sll_len) > 0)
printf("Success!\n");
else
printf("Error, could not send\n");
data_len=sizeof(buff);
printf("\tData:");
for (i=0; i<data_len; i++) printf("%c", buff[i]);
printf("\tDone: \n");
close(s);
return 0;
}
Related
Tcp socket using C (server)
I coded a program in C of a tcp socket but once executed, the server returns an error from the accept function but i can't find out why.. If you guys can help me, that would be appreciated. Also, if you have any advice on the way i programmed this, please feel free to tell me. Here is my code : #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <netdb.h> #include <arpa/inet.h> #include <netinet/in.h> int main(int argc, char *argv[]) { int listenSocket, status, socketClient; unsigned short int msgLength; struct addrinfo hints, *servinfo; struct sockaddr_in clientAddress; socklen_t clientAddressLength = sizeof clientAddress; char msg[101]; //Test port number if (argc != 2) { fprintf(stderr,"Usage : %s [NUMERO_PORT]\n",argv[0]); return 2; } memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; // IPv4 hints.ai_socktype = SOCK_DGRAM; // UDP hints.ai_flags = 0; //Car on fait le test sur la meme machine if ((status = getaddrinfo(NULL, argv[1], &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); return 3; } if ((listenSocket = socket(servinfo->ai_family, servinfo- >ai_socktype, servinfo->ai_protocol)) == -1) { perror("socket:"); return 4; } if (bind(listenSocket, servinfo->ai_addr, servinfo->ai_addrlen) == -1) { close(listenSocket); perror("bind:"); return 5; } listen(listenSocket,5); int sizeOfSockAddr = sizeof(clientAddress); socketClient= accept(listenSocket, NULL, NULL); if (socketClient < 0) { fprintf(stderr,"Erreur accept\n"); return 6; } freeaddrinfo(servinfo); printf("Waiting for a client's request %s\n", argv[1]); while (1) { //some things } }`
C socket bind slows down when i switch source ip addresses
I have a C script that connects to a remote server with a socket and writes a command. I have to do this as fast as possible and i need to switch from source ip addressess. The problem is, when i switch from ip addresses, the bind slows down for seconds. I can not find a solution. the code: #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <arpa/inet.h> int main() { struct sockaddr_in source, destination = {}; //two sockets declared as previously int sock = 0; int n = 0; int datalen = 0; int pkt = 0; char* ips[3] = {"10.0.0.1", "10.0.0.2", "10.0.0.3"}; uint8_t *send_buffer; char recv_buffer[11]; struct sockaddr_storage fromAddr; // same as the previous entity struct sockaddr_storage serverStorage; unsigned int addrlen; //in the previous example socklen_t addr_size; struct timeval tv; tv.tv_sec = 3; /* 3 Seconds Time-out */ tv.tv_usec = 0; /*Inititalize source to zero*/ memset(&source, 0, sizeof(source)); //source is an instance of sockaddr_in. Initialization to zero /*Inititalize destinaton to zero*/ memset(&destination, 0, sizeof(destination)); /* setting the destination, i.e our OWN IP ADDRESS AND PORT */ destination.sin_family = AF_INET; // destination.sin_addr.s_addr = inet_addr("123.456.789.123"); destination.sin_port = htons(43); /*---- Configure settings of the source address struct, WHERE THE PACKET IS COMING FROM ----*/ /* Address family = Internet */ source.sin_family = AF_INET; /* Set IP address to localhost */ // source.sin_addr.s_addr = INADDR_ANY; /* Set port number, using htons function to use proper byte order */ source.sin_port = htons(43); /* Set all bits of the padding field to 0 */ memset(source.sin_zero, '\0', sizeof source.sin_zero); //optional int i; for (i=0; i<60; i++) { /* creating the socket */ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) printf("Failed to create socket\n"); /*set the socket options*/ setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); if(inet_pton(AF_INET, ips[i%3], &source.sin_addr)<=0) //this is where is switch the ip addresses { printf("\n inet_pton error occured\n"); return 1; } /*bind socket to the source WHERE THE PACKET IS COMING FROM*/ if (bind(sock, (struct sockaddr *) &source, sizeof(source)) < 0) printf("Failed to bind socket"); if(inet_pton(AF_INET, "94.198.154.139", &destination.sin_addr)<=0) { printf("\n inet_pton error occured\n"); return 1; } if(connect(sock, (struct sockaddr *)&destination, sizeof(destination)) < 0) { printf("\n Error : Connect Failed \n"); return 1; } printf("check\n"); n = write(sock,"is liveresults.nl\r\n",21); if (n < 0) error("ERROR writing to socket"); while ( (n = read(sock, recv_buffer, sizeof(recv_buffer)-1)) > 0) { recv_buffer[n] = 0; if(fputs(recv_buffer, stdout) == EOF) { printf("\n Error : Fputs error\n"); } } if(n < 0) { printf("\n Read error \n"); } close(sock); } return 0; } edit: i must notice that it slows down afther the first loops for every ip. Because i have 3 ip addresses. After the first round, it slow down for seconds per bind.
I have to bind the local port to 0 instead of the same port as the outgoing port.
Coding UDP Socket in C - sendto Invalid Arguments
I'm actually back to programming in C, and I want to code a UDP Client. My problem is that I'm having an error when executing the sendto function... getting errno : 22 and the message error : Invalid argument char query[1024]; int querySize = strlen(query); SOCKADDR_IN dest = { 0 }; int destSize = sizeof dest; dest.sin_family = AF_INET; dest.sin_addr.s_addr = inet_addr('192.168.0.3'); dest.sin_port = htons(6000); sendto(sock, query, querySize, 0, (SOCKADDR *) &dest, destSize) Hope someone could help me? Here is my full code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #if defined (WIN32) #include <winsock2.h> typedef int socklen_t; #elif defined (linux) #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define closesocket(param) close(param) typedef int SOCKET; typedef struct sockaddr_in SOCKADDR_IN; typedef struct sockaddr SOCKADDR; #endif int main(int argc, char *argv[]) { #if defined (WIN32) WSADATA WSAData; WSAStartup(MAKEWORD(2,2), &WSAData); #endif char source_ip[15] = "192.168.0.20"; int source_port = 5000; char query[1024]; printf("- Opening Socket\n"); SOCKET sock; sock = socket(AF_INET, SOCK_DGRAM, 0); if(sock == INVALID_SOCKET) { perror("[ERROR] socket()"); exit(errno); } printf("- Configuring socket source to : [%s:%d]\n", source_ip, source_port); SOCKADDR_IN source; source.sin_family = AF_INET; source.sin_addr.s_addr = inet_addr(source_ip); source.sin_port = htons(source_port); if(bind(sock, (SOCKADDR *)&source, sizeof(source)) == SOCKET_ERROR) { perror("[ERROR] bind()"); exit(errno); } int querySize = strlen(query); SOCKADDR_IN dest = { 0 }; int destSize = sizeof dest; dest.sin_family = AF_INET; printf("- Sending packets\n"); dest.sin_addr.s_addr = inet_addr('192.168.0.3'); dest.sin_port = htons(6000); if(sendto(sock, query, querySize, 0, (SOCKADDR *) &dest, destSize) < 0) { perror("[ERROR] sendto()"); printf("%d\n", errno); exit(errno); } printf("\n\n##############################\n"); printf("Closing socket ...\n"); closesocket(sock); #if defined (WIN32) WSACleanup(); #endif printf("Program finished.\n"); return 0; }
Did you notice, that query is not being initialized? So strlen(query) might result in a "very long" buffer. That would be a good candidate for an EINVAL.
How to use select() in UDP Sockets to read and write from client to server
I'm trying to make a chat application using sockets, and I want them both to be able to write and read simultaneously. So I did some research and found that I/O multiplexing like select() will fix this problem. But I still have one problem. After I compile it and run it, one of the two programs doesn't receive the message 'till after one responds. So for example, the server sends a message to the client right? Then the client won't see the message until AFTER the client responds. Below you can find an example of this as well as the source code. Client: hi Server: hello Client said "hi" And the same for client etc //SERVER #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <stdlib.h> #include <sys/select.h>//use select() for multiplexing #include <sys/fcntl.h> // for non-blocking #define MAX_LENGTH 1024 /* Select() params * int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); * FD_SET(int fd, fd_set *set); * FD_CLR(int fd, fd_set *set); * FD_ISSET(int fd, fd_set *set); * FD_ZERO(fd_set *set); */ void error(char *message) { perror(message); exit(1); } int main() { fd_set original_socket; fd_set original_stdin; fd_set readfds; fd_set writefds; struct timeval tv; int numfd; int socket_fd, bytes_read; unsigned int address_length; char recieve_data[MAX_LENGTH],send_data[MAX_LENGTH]; struct sockaddr_in server_address , client_address; if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { error("socket()"); } int flags = fcntl(socket_fd, F_GETFL); flags |= O_NONBLOCK; fcntl(socket_fd, F_SETFL, flags); //fcntl(socket_fd, F_SETFL, O_NONBLOCK); //set socket to non-blocking // clear the set ahead of time FD_ZERO(&original_socket); FD_ZERO(&original_stdin); FD_ZERO(&readfds); FD_ZERO(&writefds); // add our descriptors to the set (0 - stands for STDIN) FD_SET(socket_fd, &original_socket);//instead of 0 put socket_fd FD_SET(socket_fd, &readfds); FD_SET(0,&original_stdin); FD_SET(0, &writefds); // since we got s2 second, it's the "greater", so we use that for // the n param in select() numfd = socket_fd + 1; // wait until either socket has data ready to be recv()d (timeout 10.5 secs) tv.tv_sec = 10; tv.tv_usec = 500000; server_address.sin_family = AF_INET; server_address.sin_port = htons(5000); server_address.sin_addr.s_addr = INADDR_ANY; bzero(&(server_address.sin_zero),8); if (bind(socket_fd,(struct sockaddr *)&server_address, sizeof(struct sockaddr)) == -1) { error("bind()"); } address_length = sizeof(struct sockaddr); printf("\nUDP_Server Waiting for client to respond...\n"); fflush(stdout); printf("Type (q or Q) at anytime to quit\n"); while (1) { readfds = original_socket; writefds = original_stdin;//problem int recieve = select(numfd, &readfds, &writefds,/*NULL,*/ NULL, &tv); if (recieve == -1) { perror("select"); // error occurred in select() } else if (recieve == 0) { printf("Timeout occurred! No data after 10.5 seconds.\n"); } else { // one or both of the descriptors have data if (FD_ISSET(socket_fd, &readfds)) //if set to read { FD_CLR(socket_fd, &readfds); bytes_read = recvfrom(socket_fd,recieve_data,MAX_LENGTH,0,(struct sockaddr *)&client_address, &address_length); //block call, will wait till client enters something, before proceeding recieve_data[bytes_read] = '\0'; //add null to the end of the buffer if((strcmp(recieve_data , "q") == 0) || (strcmp(recieve_data , "Q") == 0)) { //if client quit, then quit also printf("\nClient has exited the chat.\n"); break; } printf("\n(%s , %d) said: %s\n",inet_ntoa(client_address.sin_addr),ntohs(client_address.sin_port),recieve_data); } else if (FD_ISSET(/*socket_fd*/0, &writefds)) //if set to write //else { FD_CLR(0, &writefds); printf("SERVER: "); fgets (send_data, MAX_LENGTH, stdin); //input the name with a size limit of MAX_LENGTH if ((strlen(send_data)>0) && (send_data[strlen (send_data) - 1] == '\n')) //if there is a trailing \n, remove it { send_data[strlen (send_data) - 1] = '\0'; } if ((strcmp(send_data , "q") == 0) || (strcmp(send_data , "Q") == 0)) { //if user enters q, then quit sendto(socket_fd,send_data,strlen(send_data),0,(struct sockaddr *)&client_address, sizeof(struct sockaddr)); break; } sendto(socket_fd,send_data,strlen(send_data),0,(struct sockaddr *)&client_address, sizeof(struct sockaddr)); fflush(stdout); } else printf("\nOOPS! What happened? SERVER"); } //end else }//end while close (socket_fd); return 0; } At first I thought it was a trailing newline, but I took care of that possibility already by changing the '/n' to '/0' if it exists. I really don't understand why this is happening! //CLIENT #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> //host struct #include <stdio.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <stdlib.h> #include <sys/select.h>//use select() for multiplexing #include <sys/fcntl.h> // for non-blocking #define MAX_LENGTH 1024 #define HOST "localhost" //127.0.0.1 void error(char *message) { perror(message); exit(1); } int main() { fd_set original_socket; fd_set original_stdin; fd_set readfds; fd_set writefds; struct timeval tv; int numfd; int socket_fd,bytes_recieved; unsigned int address_length; struct sockaddr_in server_address; struct hostent *host; char send_data[MAX_LENGTH],recieve_data[MAX_LENGTH]; host = (struct hostent *) gethostbyname((char *)HOST);//127.0.0.1 if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { error("socket()"); } int flags = fcntl(socket_fd, F_GETFL); flags |= O_NONBLOCK; fcntl(socket_fd, F_SETFL, flags); //fcntl(socket_fd, F_SETFL, O_NONBLOCK); //set socket to non-blocking // clear the set ahead of time FD_ZERO(&original_socket); FD_ZERO(&original_stdin); FD_ZERO(&readfds); FD_ZERO(&writefds); // add our descriptors to the set (0 - stands for STDIN) FD_SET(socket_fd, &original_socket);//instead of 0 put socket_fd FD_SET(socket_fd, &readfds); FD_SET(0,&original_stdin); FD_SET(0, &writefds); // since we got s2 second, it's the "greater", so we use that for // the n param in select() numfd = socket_fd + 1; // wait until either socket has data ready to be recv()d (timeout 10.5 secs) tv.tv_sec = 10; tv.tv_usec = 500000; server_address.sin_family = AF_INET; server_address.sin_port = htons(5000); server_address.sin_addr = *((struct in_addr *)host->h_addr); bzero(&(server_address.sin_zero),8); address_length = sizeof(struct sockaddr); printf("Type (q or Q) at anytime to quit\n"); while (1) { readfds = original_socket; writefds = original_stdin;//problem int recieve = select(numfd, &readfds, /*NULL*/&writefds, NULL, &tv); if (recieve == -1) { perror("select"); // error occurred in select() } else if (recieve == 0) { printf("Timeout occurred! No data after 10.5 seconds.\n"); } else { // one or both of the descriptors have data if (FD_ISSET(socket_fd, &readfds)) //if set to read { FD_CLR(socket_fd, &readfds);//clear the set bytes_recieved = recvfrom(socket_fd,recieve_data, sizeof(recieve_data),0,(struct sockaddr *)&server_address,&address_length); recieve_data[bytes_recieved]= '\0'; if((strcmp(recieve_data , "q") == 0) || (strcmp(recieve_data , "Q") == 0)) //if client quit, then quit also { printf("\nServer has exited the chat.\n"); break; } printf("\n(%s , %d) said: %s\n",inet_ntoa(server_address.sin_addr),ntohs(server_address.sin_port),recieve_data); //inet_ntoa returns an ip address ipv4 style, ex: 127.0.0.1, and ntohs returns the port in the converted byte ordering } else if (FD_ISSET(0/*socket_fd*/, &writefds)) //if set to write //else { FD_CLR(0, &writefds); printf("ClIENT: "); fgets (send_data, MAX_LENGTH, stdin); if ((strlen(send_data)>0) && (send_data[strlen (send_data) - 1] == '\n')) //remove trailing newline, if exists { send_data[strlen (send_data) - 1] = '\0'; } if ((strcmp(send_data , "q") == 0) || strcmp(send_data , "Q") == 0) //if user quits, then send an invisible message to server to quit also { sendto(socket_fd, send_data, strlen(send_data), 0, (struct sockaddr *)&server_address, sizeof(struct sockaddr)); break; } else { sendto(socket_fd, send_data, strlen(send_data), 0, (struct sockaddr *)&server_address, sizeof(struct sockaddr)); } } else printf("\nOOPS! What happened? CLIENT"); } //end else } // end while close (socket_fd); }
Your mistake is that with select() you wait for the socket to be readable (which is right) and STDIN to be writable (which is wrong, since the terminal is writable most of the time). You have to wait for both socket or STDIN to be readable, i. e. both file descriptors are to be set in readfds, and NULL can be passed instead of &writefds.
Sending RAW data to IP (TCP/IP)
I'm trying to learn some WinSock and stuck in place. I want to sent RAW packet (256 bytes) to another device in the network. This device is just a listener. I'm using VC++ 2010 and WinSock. So there a code I'm working on: // winsock.cpp : main project file. #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <Windows.h> #include <string.h> //#include <WinSock2.h> #define WIN32_LEAN_AND_MEAN using namespace System; int main(array<System::String ^> ^args) { WORD RequiredVersion; WSADATA WData; SOCKET s; struct sockaddr_in addr; long val; struct hostent *he; char host[128]; int port; printf("HOST: "); scanf("%s",host); printf("PORT: "); scanf("%d",&port); printf("Data to send: "); scanf("%d",&val); RequiredVersion = MAKEWORD(2, 0); if (WSAStartup(RequiredVersion, &WData) != 0) { printf("ERROR! WinSock2\n"); return 1; } //he = gethostbyaddr((char *) host, 4, AF_INET); he = gethostbyname(host); if (he == NULL) { printf("host not found.\n"); system("PAUSE"); return 1; } addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = *((unsigned long*) he->h_addr); s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); //SOCK_RAW, IPPROTO_TCP); connect(s, (struct sockaddr*) &addr, sizeof(addr)); printf("SENDING %d TO %s\n",val,inet_ntoa(addr.sin_addr)); val = htonl(val); send(s, (char*) &val, sizeof(long), 0); printf("WAITING FOR RESPONSE...\n"); recv(s, (char*) &val, sizeof(long), 0); val = ntohl(val); printf("RE: %d\n", val); closesocket(s); WSACleanup(); system("PAUSE"); return 0; } The problems I've got: I'm probably not creating a real RAW socket. I think I should not use 'gethostbyname', but I don't know how to replace it for an const. IP My program doesn't sent any data over LAN. I'm using WireShark to check this. I think this can be a problem with 'gethostbyname' Any tips? I really appreciate any help you can provide!