【问题标题】:Socket: Protocol not supported error套接字:协议不支持错误
【发布时间】:2014-02-10 18:44:28
【问题描述】:

我目前正在 Linux 中使用 C 语言进行原始套接字编程。当我编译程序时,没有检测到错误。但是,在运行程序时,我收到“套接字:不支持协议”错误。我正在使用的代码如下所示。有人可以帮我找到问题的解决方案吗?

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/tcp.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<netdb.h>
#define P 25

struct tcph {
unsigned short int th_sport;
unsigned short int th_dport;
 unsigned int th_seq;
 unsigned int th_ack;
 unsigned char th_x2:4, th_off:4;
 unsigned char th_flags;
 unsigned short int th_win;
 unsigned short int th_sum;
 unsigned short int th_urp;
};

int main()
{
 int sock,connected,bytes_recieved,true=1;
char send_data[1024],recv_data[1024];
struct sockaddr_in server_addr,client_addr;
int sin_size;

char datagram[4096];
struct tcph *tcph = (struct tcph *) datagram;
tcph->th_sport = htons (1234);
tcph->th_dport = htons (P);
tcph->th_seq = random ();
tcph->th_ack = 0;
tcph->th_x2 = 0;
tcph->th_off = 0;       
tcph->th_flags = 0; 
tcph->th_win = htonl (65535);   
tcph->th_sum = 0;
tcph->th_urp = 0;

if((sock=socket(AF_INET,SOCK_RAW,0))==-1)
{
perror("Socket");
exit(1);
}
if(setsockopt(sock,IPPROTO_TCP,IP_HDRINCL,&true,sizeof(true))==-1)
{
perror("Setsockopt");
exit(1);
}
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(9392);
server_addr.sin_addr.s_addr=INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if(bind(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
{
perror("Unable to bind");
exit(1);
}
if(listen(sock,5)==-1)
{
perror("Listen");
exit(1);
}
printf("\nTCP Server waiting for client on port 9392");
fflush(stdout);
while(1)
{
 sin_size=sizeof(struct sockaddr_in);
 connected=accept(sock,(struct sockaddr *)&client_addr,&sin_size);
 printf("\nI got a connection from (%s,%d)",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
 while(1)
{
printf("\nSEND (q or Q to quit):");
scanf("%s",send_data);
if(strcmp(send_data,"q")==0||strcmp(send_data,"Q")==0)
{
send(connected,send_data,strlen(send_data),0);
close(connected);
exit(0);
}
else
send(connected,send_data,strlen(send_data),0);
bytes_recieved=recv(connected,recv_data,1024,0);
recv_data[bytes_recieved]='\0';
if(strcmp(recv_data,"q")==0||strcmp(recv_data,"Q")==0)
{
close(connected);
break;
}
else
{
printf("\nRECIEVED DATA=%s",recv_data);
}
fflush(stdout);
}
}
close(sock);
return 0;
}

【问题讨论】:

    标签: linux sockets tcp


    【解决方案1】:

    您可能想看看 D.J. Bernstein 的 tcpserver(参见 http://cr.yp.to/ucspi-tcp/tcpserver.html)。基本上,您可以简单地在 tcpserver 下运行您的 C 程序,tcpserver 将处理所有事情,包括设置套接字、列出您正在使用的任何端口上的传入连接等。当传入连接到达您指定的端口时, tcpserver 将生成程序的一个实例,并将来自客户端的传入信息通过管道传输到程序的 STDIN,并将来自程序的 STDOUT 的传出信息传输回客户端。这样,您可以专注于程序的核心逻辑(并简单地读取/写入 stdout/stdin),并让 tcpserver 处理所有繁重的工作,例如套接字等。

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 2013-02-04
        • 1970-01-01
        • 2013-11-12
        • 2014-11-23
        • 1970-01-01
        • 1970-01-01
        • 2021-05-04
        • 2020-04-23
        • 1970-01-01
        相关资源
        最近更新 更多