Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8541343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:48:37+00:00 2026-06-11T11:48:37+00:00

soc = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); in client side, when run, get == -1 error

  • 0
  1. soc = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); in client side, when run, get == -1 error

  2. if accept function can only use with UDP and TCP higher protocol, how to accept multiple client with layer 2 communication?

where can find the code of accept function, i would like to rewrite it for layer 2.

Updated :
After tried soc = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
also == -1 , get this error

server side and client side both are the same computer, local one
strange is that running server side, it do not have this error, but running client program got error

//#include "stdafx.h"

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h> 
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
//#include "sock.h"

#define MAX_MESSAGE          21000
#define FD_NUM 5
#define tcp_port                5009


//#pragma comment(lib, "ws2_32.lib")
//#include <winsock2.h>

char host_ip[16] = "127.0.0.1";

void task()
{
    struct sockaddr_in local;
    int opt;
    int soc;

    //soc = socket(AF_INET,SOCK_STREAM,0);
    soc = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (soc==-1) {
        printf("socket error\n");
    }
    // determine ethernet number    
    /*
    struct ifreq ifr;
    size_t if_name_len=strlen(if_name);
    if (if_name_len<sizeof(ifr.ifr_name)) {
        memcpy(ifr.ifr_name,if_name,if_name_len);
        ifr.ifr_name[if_name_len]=0;
    } else {
        printf("interface name is too long");
    }
    if (ioctl(fd,SIOCGIFINDEX,&ifr)==-1) {
        printf("determine ethernet number error\n");
    }
    int ifindex=ifr.ifr_ifindex;
    */
    // mac address
    /*target address*/
    struct sockaddr_ll socket_address;

    /*buffer for ethernet frame*/
    void* buffer = (void*)malloc(ETH_FRAME_LEN);

    /*pointer to ethenet header*/
    unsigned char* etherhead = (unsigned char*)buffer;

    /*userdata in ethernet frame*/
    unsigned char* data = (unsigned char*)buffer + 14;

    /*another pointer to ethernet header*/
    struct ethhdr *eh = (struct ethhdr *)etherhead;

    int send_result = 0;

    /*our MAC address*/
    //10:78:d2:ad:90:cb
    //0x10,0x78,0xD2,0xAD,0x90,0xCB
    unsigned char src_mac[6] = {0x10,0x78,0xD2,0xAD,0x90,0xCB};

    /*other host MAC address*/
    unsigned char dest_mac[6] = {0x10,0x78,0xD2,0xAD,0x90,0xCB};

    /*prepare sockaddr_ll*/

    /*RAW communication*/
    socket_address.sll_family   = PF_PACKET;    
    /*we don't use a protocoll above ethernet layer
      ->just use anything here*/
    socket_address.sll_protocol = htons(ETH_P_IP);  

    /*index of the network device
    see full code later how to retrieve it*/
    socket_address.sll_ifindex  = 0;

    /*ARP hardware identifier is ethernet*/
    socket_address.sll_hatype   = ARPHRD_ETHER;

    /*target is another host*/
    socket_address.sll_pkttype  = PACKET_OTHERHOST;

    /*address length*/
    socket_address.sll_halen    = ETH_ALEN;     
    /*MAC - begin*/
    socket_address.sll_addr[0]  = 0x10;     
    socket_address.sll_addr[1]  = 0x78;     
    socket_address.sll_addr[2]  = 0xD2;
    socket_address.sll_addr[3]  = 0xAD;
    socket_address.sll_addr[4]  = 0x90;
    socket_address.sll_addr[5]  = 0xCB;
    /*MAC - end*/
    socket_address.sll_addr[6]  = 0x00;/*not used*/
    socket_address.sll_addr[7]  = 0x00;/*not used*/

    memcpy((void*)buffer, (void*)dest_mac, ETH_ALEN);
    memcpy((void*)(buffer+ETH_ALEN), (void*)src_mac, ETH_ALEN);
    eh->h_proto = 0x00;

    int j = 0;
    for (j = 46; --j; data[j] = (unsigned char)((int) (255.0*rand()/(RAND_MAX+1.0))));

    /*
    struct sockaddr_in server;
    int len = sizeof(server);
    server.sin_family=AF_INET;
    server.sin_port=htons(5008);
    server.sin_addr.s_addr=inet_addr(host_ip);

    int CONN_SOCK   = InitSocketTcp(tcp_port);
    if(connect(CONN_SOCK, (struct sockaddr*)&server, sizeof(server)) == -1)
    {
        printf("connection failed\n");
    }
    else
    {
            printf("connection ok!\n");
    }
    */
    while(1)
    {
        char buff[492] = "\0";
        printf("input: ");
        scanf("%s", buff);

        //send(CONN_SOCK,buff,strlen(buff),0);
        /*send the packet*/
        send_result = sendto(soc, buff, ETH_FRAME_LEN, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
        send_result == -1?printf("send error"):0;

        if(buff[0] == 'q')
        {
            //shutdown(CONN_SOCK, SD_SEND);
            //closesocket(CONN_SOCK);
            //WSACleanup();
            close(soc);
            exit(0);
        }
    }
}

int main()
{
    //for(int i=10; i!=0; --i)
        //pthread_create();
    task();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:48:38+00:00Added an answer on June 11, 2026 at 11:48 am

    Accept() is only used for TCP or UDP (practically it’s main use is in tcp), because it establishes a connection. A connection does 3 way handshake in case of tcp and exchanges information such as sequence numbers etc. and is completely identified by a socket (port plus ip address)

    In contrast to that you can simply use sendto and receivefrom api’s as normally used in case of udp, where each packet may follow a different path to reach destination. You do not require an accept in case of udp communication. The same can be extended to link layer (L2) frames i.e. each side can send or receive at will without actually establishing a connection first.

    This should be done using root

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to send a txt file via socket connection when requested by client.
I am using a JQuery autocomplete AJAX plugin as follows; $(document).ready(function() { $(#myfield).autocomplete({ serviceUrl:'autocomplete.asp?soc='
i have a simple http client server. here client can download from the server.
The mysql query result can have 1 or 2 rows with results. How would
For educational purposes I tried to make a server and client where the server
today i came across a new app-store for Android called soc.io. The website looks
I've got a Ruby TCPSocket client that works great except when I'm trying to
How can I assign the db_owner role to a user that I have created?
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
I try to save user's birth date, but get null value in column dob

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.