c++ - Unix server - Windows client, connection failed -
is there more required in order communicate server unix process , client windows process? after compiling both, run server , run client. however, client fails @ connect() error: 10061.
client (windows application):
#ifndef unicode #define unicode #endif #define win32_lean_and_mean #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> // need link ws2_32.lib. #pragma comment(lib, "ws2_32.lib") int wmain() { // initialize winsock. wsadata wsadata; int iresult = wsastartup(makeword(2, 2), &wsadata); if (iresult != no_error) { printf("wsastartup() failed error: %d\n", iresult); return 1; } // create socket connecting server. socket connectsocket; connectsocket = socket(af_inet, sock_stream, ipproto_tcp); if (connectsocket == invalid_socket) { printf("socket() failed error: %ld\n", wsagetlasterror()); wsacleanup(); return 1; } // sockaddr_in structure specifies address family, // ip address, , port of server connected to. sockaddr_in service; memset(&service, 0, sizeof(service)); service.sin_family = af_inet; service.sin_addr.s_addr = inet_addr("127.0.0.1"); service.sin_port = htons(27015); // connect server. iresult = connect(connectsocket, (sockaddr *) &service, sizeof (service)); if (iresult == socket_error) { printf("connect() failed error: %ld\n", wsagetlasterror()); iresult = closesocket(connectsocket); if (iresult == socket_error) printf("closesocket() failed error: %ld\n", wsagetlasterror()); wsacleanup(); return 1; } // message has sent. char message[1000]; printf("\nenter message: "); gets_s(message); printf("message wrote is: %s\n", message); // send message. if (send(connectsocket, message, sizeof(message), 0) == socket_error) { printf("send() failed error code: %d\n", wsagetlasterror()); } printf("message sent server."); closesocket(connectsocket); wsacleanup(); while(1); return 0; }
server (unix application):
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <time.h> #include <gnu/stubs-64.h> int main(int argc, char *argv[]) { int n; int listenfd = 0, connfd = 0; struct sockaddr_in serv_addr; char sendbuff[1025]; listenfd = socket(af_inet, sock_stream, 0); memset(&serv_addr, '0', sizeof(serv_addr)); memset(sendbuff, '0', sizeof(sendbuff)); serv_addr.sin_family = af_inet; serv_addr.sin_addr.s_addr = htonl(inaddr_any); serv_addr.sin_port = htons(27015); bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); listen(listenfd, 10); while(1) { connfd = accept(listenfd, (struct sockaddr*)null, null); n = read(connfd,sendbuff,255); printf("here message: %s\n",sendbuff); close(connfd); sleep(1); } }
client running on windows 7, while server on fedora 19 (vmware). port ok when run client-server windows applications. also, have no anti-virus sw installed. appreciated.
you getting connection refused error because client connecting client machine, not server machine.
in client code, replace address 127.0.0.1 server's address.
Comments
Post a Comment