【发布时间】:2022-11-13 01:30:46
【问题描述】:
我设法制作了一个可重现的示例(我的大型原始源代码中的所有内容都保留了下来)。问题是只发送了第一个字母a。然后,我得到send() failed: Connection refused。我不知道该怎么做,这实际上是应该工作的最小代码。
我应该说,只有当我在程序开始时打开套接字并在程序结束时关闭它时,代码才起作用。如果我为每个单独的 send() 打开一个套接字,然后立即关闭它,则不会出现错误。
#include <pcap.h>
#include <cstdio>
#include <getopt.h>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <map>
#include <vector>
#define BUFSIZE 256
#define __FAVOR_BSD
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include<netdb.h>
#include<err.h>
#include <arpa/inet.h>
#include <netinet/ether.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp6.h>
using namespace std;
#define BUFFER 1024 // buffer length
int sock; // socket descriptor
void start_connection(){
struct sockaddr_in server, from; // address structures of the server and the client
struct hostent *servent; // network host entry required by gethostbyname()
memset(&server,0,sizeof(server)); // erase the server structure
server.sin_family = AF_INET;
// make DNS resolution of the first parameter using gethostbyname()
if ((servent = gethostbyname("127.0.0.1")) == NULL) // check the first parameter
errx(1,"gethostbyname() failed\n");
// copy the first parameter to the server.sin_addr structure
memcpy(&server.sin_addr,servent->h_addr,servent->h_length);
server.sin_port = htons(2055);
if ((sock = socket(AF_INET , SOCK_DGRAM , 0)) == -1) //create a client socket
err(1,"socket() failed\n");
// create a connected UDP socket
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == -1)
err(1, "connect() failed");
}
void send_data(char * string){
char buffer[BUFFER];
int msg_size,i;
//send data to the server
memcpy(buffer,string,2);
msg_size=2;
i = send(sock,buffer,msg_size,0); // send data to the server
if (i == -1) // check if data was sent correctly
err(1,"send() failed");
else if (i != msg_size)
err(1,"send(): buffer written partially");
printf("%s",string);
}
int main(int argc, char *argv[])
{
//Start UDP connection
start_connection();
send_data("a");
send_data("b");
send_data("c");
//Close the UDP connection
close(sock);
return 0;
}
【问题讨论】: