मेरे पास डेटा अधिग्रहण मॉड्यूल है जिसमें से मैं ईथरनेट पोर्ट से डेटा एकत्र करना चाहता हूं। मैं चरणों में वहां जा रहा हूं, वर्तमान में मैं क्लाइंट से सर्वर पर केवल connect
करना चाहता हूं। मैंने मूल सी कोड प्राप्त करने के लिए बीज की मार्गदर्शिका का उपयोग किया है। लेकिन मैं सिर्फ इस कनेक्ट त्रुटि connect: Connection refused.
सी, सॉकेट्स: कनेक्शन अस्वीकार त्रुटि
हो रही यह मुझे क्या करना है रखें:
नेटवर्क IP यहाँ उल्लेख स्थैतिक IP जो मैं कॉन्फ़िगर किया गया है।
पोर्ट संख्या सर्वर साइड पर और क्लाइंट साइड मैं बंदरगाह 50000.
मैं निर्माण और सर्वर साइड एप्लिकेशन को चलाने के लिए और फिर पर इस आईपी से कनेक्ट से 50000 के लिए सेट है इसे करने के लिए कनेक्ट करने का प्रयास क्लाइंट साइड एप्लिकेशन चलाकर।
एक सर्वर साइड, सर्वर साइड आवेदन returns
इससे पहले कि मैं ग्राहक के पक्ष अनुप्रयोग प्रारंभ के बारे में संदेह है, इसलिए मैं इसे चलाने (while(1);
) ताकि मैं ग्राहक की ओर से इसे करने के लिए कनेक्ट कर सकते हैं रखना चाहिए?
क्या गलत हो रहा है क्या मैं यहां कुछ भूल रहा हूं? मदद!
मैं बहुत थोड़ा संशोधित (आईपी और पोर्ट संख्या अलग हैं) बीज के सर्वर साइड और क्लाइंट साइड यहाँ सी कोड को पेस्ट कर रहा हूँ:
Server.c
/*
** server.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
// code for a server waiting for connections
// namely a stream socket on port 3490, on this host's IP
// either IPv4 or IPv6.
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP address
if ((rv = getaddrinfo(NULL, "50000", &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("socket");
continue;
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("bind");
continue;
}
break; // if we get here, we must have connected successfully
}
if (p == NULL)
{
// looped off the end of the list with no successful bind
fprintf(stderr, "failed to bind socket\n");
exit(2);
}
freeaddrinfo(servinfo); // all done with this structure
}
Client.c
/*
** client.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
// code for a client connecting to a server
// namely a stream socket to www.example.com on port 80 (http)
// either IPv4 or IPv6
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo("192.168.2.4", "50000", &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("connect");
continue;
}
break; // if we get here, we must have connected successfully
}
if (p == NULL)
{
// looped off the end of the list with no connection
fprintf(stderr, "failed to connect\n");
exit(2);
}
freeaddrinfo(servinfo); // all done with this structure
}
लिंक वे एक अच्छा पढ़ा रहे हैं के लिए धन्यवाद। – HaggarTheHorrible
@HeatfanJohn मैं एक कोड है कि इन किया है की जाँच की है, लेकिन एक ही समस्या परिणाम [server.c] (https://paste.ubuntu.com/25599389/) [client.c] (https:। //paste.ubuntu। com/25599391 /) – alhelal
@BandaMuhammadAlHelal आपका सर्वर पोर्ट 9999 पर सुन किया जाएगा, जबकि अपने ग्राहक बंदरगाह FTP_PORT (21) से कनेक्ट करने के प्रयास कर रहा है प्रकट होता है। कनेक्ट करने के लिए पोर्ट 99 99 का उपयोग करने के लिए अपने क्लाइंट को बदलें। – HeatfanJohn