- This topic has 2 replies, 1 voice, and was last updated 16 years, 7 months ago by jarome.
-
AuthorPosts
-
jaromeMemberI am trying to get sockets to work in Windows using the MinGW environment. The following program is from Microsoft. No matter what I do, I cannot get MyEclipseIDE to include the winsock2.h include file. It does not complain until it gets to the definition of sockaddr_in which is defined in the include file.
1) Is there a way to get a listing created that shows the results after the preprocessor step so I can tell what is happening?
2) How do I link against a library (.a file) as opposed to a dll? MinGW has the libws2_32.a file which hopefully includes the socket routines, but I cannot figure out how to get MyEclipseIDE to use them. I can use the ws2_s2.dll file in the visualStudio directory, but I would rather stick to free things.
Thanks for the help,
Jim
———————// Microsoft Development Environment 2003 - Version 7.1.3088 // Copyright (r) 1987-2002 Microsoft Corporation. All Right Reserved // Microsoft .NET Framework 1.1 - Version 1.1.4322 // Copyright (r) 1998-2002 Microsoft Corporation. All Right Reserved // // Run on Windows XP Pro machine, version 2002, SP 2 // // <windows.h> already included // WINVER = 0x0501 for Xp already defined in windows.h // A sample of client program #include <stdio.h> #include <winsock2.h> int main() { // Initialize Winsock. WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) printf("Client: Error at WSAStartup().\n"); else printf("Client: WSAStartup() is OK.\n"); // Create a socket. SOCKET m_socket; m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (m_socket == INVALID_SOCKET) { printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError()); WSACleanup(); return 0; } else printf("Client: socket() is OK.\n"); // Connect to a server. sockaddr_in clientService; //### It does not know sockaddr_in from winsock2.h clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr("127.0.0.1"); clientService.sin_port = htons(55555); if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR) { printf("Client: connect() - Failed to connect.\n"); WSACleanup(); return 0; } // Send and receive data. int bytesSent; int bytesRecv = SOCKET_ERROR; // Be careful with the array bound, provide some checking mechanism char sendbuf[200] = "Client: Sending some test string to server..."; char recvbuf[200] = ""; bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0); printf("Client: send() - Bytes Sent: %ld\n", bytesSent); while(bytesRecv == SOCKET_ERROR) { bytesRecv = recv(m_socket, recvbuf, 32, 0); if (bytesRecv == 0 || bytesRecv == WSAECONNRESET) { printf("Client: Connection Closed.\n"); break; } else printf("Client: recv() is OK.\n"); if (bytesRecv < 0) return 0; else printf("Client: Bytes received - %ld.\n", bytesRecv); } return 0; }
jaromeMemberAnd yes, I did put the mingw\include in my path
jaromeMemberin the compiler’s include path
-
AuthorPosts