D
Dom
I'm new to c++. Just started learning it 24 hours ago. Am running into a
compile problem. Please, no one waste the effort telling me to google it.
I've been researching it for quite a while with no joy. I got dev-c++ and a
bit of winsock sample code. I've done nothing out of the ordinary. I could
only assume that anyone else that downloaded this software and attempted
this would meet with the same result. The problem lies with either the
compiler or the source. I'm not sure which. Help greatly appreciated.
Compiler output and source to follow.
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\d\My Documents\c\test.cpp" -o
"C:\Documents and Settings\d\My
uments\c\test.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward"
-I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2"
-I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
.text+0x46): undefined
reference to `WSAStartup@8'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
.text+0x80): undefined
reference to `socket@12'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
.text+0x97): undefined
reference to `WSAGetLastError@0'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
.text+0xac): undefined
reference to `WSACleanup@0'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
.text+0xd0): undefined
reference to `inet_addr@4'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
.text+0xe5): undefined
reference to `htons@4'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
.text+0x10f): undefined
reference to `connect@12'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
.text+0x128): undefined
reference to `WSACleanup@0'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
.text+0x1e5): undefined
reference to `send@16'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
.text+0x239): undefined
reference to `recv@16'
collect2: ld returned 1 exit status
Execution terminated
#include <stdio.h>
#include "winsock2.h"
int main() {
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");
// Create a socket.
SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return 0;
}
// Connect to a server.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( 27015 );
if ( connect( m_socket, (SOCKADDR*) &clientService,
sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return 0;
}
// Send and receive data.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "Client: Sending data.";
char recvbuf[32] = "";
bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
printf( "Bytes Sent: %ld\n", bytesSent );
while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( m_socket, recvbuf, 32, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Connection Closed.\n");
break;
}
if (bytesRecv < 0)
return 0;
printf( "Bytes Recv: %ld\n", bytesRecv );
}
return 0;
}
compile problem. Please, no one waste the effort telling me to google it.
I've been researching it for quite a while with no joy. I got dev-c++ and a
bit of winsock sample code. I've done nothing out of the ordinary. I could
only assume that anyone else that downloaded this software and attempted
this would meet with the same result. The problem lies with either the
compiler or the source. I'm not sure which. Help greatly appreciated.
Compiler output and source to follow.
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\d\My Documents\c\test.cpp" -o
"C:\Documents and Settings\d\My
uments\c\test.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward"
-I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2"
-I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
reference to `WSAStartup@8'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
reference to `socket@12'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
reference to `WSAGetLastError@0'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
reference to `WSACleanup@0'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
reference to `inet_addr@4'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
reference to `htons@4'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
reference to `connect@12'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
reference to `WSACleanup@0'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
reference to `send@16'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp
reference to `recv@16'
collect2: ld returned 1 exit status
Execution terminated
#include <stdio.h>
#include "winsock2.h"
int main() {
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");
// Create a socket.
SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return 0;
}
// Connect to a server.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( 27015 );
if ( connect( m_socket, (SOCKADDR*) &clientService,
sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return 0;
}
// Send and receive data.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "Client: Sending data.";
char recvbuf[32] = "";
bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
printf( "Bytes Sent: %ld\n", bytesSent );
while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( m_socket, recvbuf, 32, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Connection Closed.\n");
break;
}
if (bytesRecv < 0)
return 0;
printf( "Bytes Recv: %ld\n", bytesRecv );
}
return 0;
}