Je suis novice en langage C et C# et j'apprécierais toute aide/rétroaction sur le problème suivant. Fondamentalement, j'essaie d'établir une connexion socket dans différents langages de programmation. le côté client a été développé en C et le côté serveur en C#. Les deux applications fonctionnent bien seulement quand j'ai testé dans le même langage de programmation, par exemple, le côté client fonctionne bien si le serveur est en C. même applique le côté serveur en C#. la rencontre d'erreur chaque fois que le client.c essaie de se connecter est « Connexion refusée » lorsque cette ligne obtient dans le codeConnexion d'un client de socket C à un serveur de socket C#
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
ici est le code pour les deux extrémités de socket.
client.c
/* Client socket demo */
int sockfd, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
char hostname[1024];
//hostname[1023] = '\0';
struct hostent* h;
gethostname(hostname, sizeof(hostname));
//printf("Hostname: %s\n", hostname);
server = gethostbyname(hostname);
//printf("h_name: %s\n", h->h_name);
//portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
//server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; // only IPv4
memcpy(&serv_addr.sin_addr, server->h_addr, server->h_length); // fixed part
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(SERVERPORT); // htons((u_short)port); .sin_addr.S_un.S_addr = htonl(INADDR_ANY)
//serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("here we are %s\n",buffer);
close(sockfd);
Server.cs
void ButtonStartListenClick(object sender, System.EventArgs e)
{
try
{
// Check the port value
if(textBoxPort.Text == "")
{
MessageBox.Show("Please enter a Port Number");
return;
}
string portStr = textBoxPort.Text;
int port = System.Convert.ToInt32(portStr);
// Create the listening socket...
m_mainSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint (IPAddress.Any, port);
// Bind to local IP Address...
m_mainSocket.Bind(ipLocal);
// Start listening...
m_mainSocket.Listen(4);
// Create the call back for any client connections...
m_mainSocket.BeginAccept(new AsyncCallback (OnClientConnect), null);
UpdateControls(true);
}
catch(SocketException se)
{
MessageBox.Show (se.Message);
}
}
Eh bien, la connexion a été refusée. Assurez-vous donc de vous connecter à la bonne adresse IP et au bon port. Vérifiez également les paramètres de votre pare-feu. – dtb