#ifdef WINDOWS_SOCKETS
#include <winsock2.h>
#else
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
Antharax said:Why don't you look at the WinSock examples (available at https://tangentsoft.net/wskfaq/ )
I know winsock is a 'strange' header file. You have to include the Winsock.h file before the Windows.h file (when you're using it too, probably in other header files).
Probably the winsock.h file already defines the structures which are also defined in the socket.h file or an other one included.
I guess you should only include the winsock.h or winsock2.h file.
And check the examples!
Code:#ifdef WINDOWS_SOCKETS #include <winsock2.h> #else #include <sys/time.h> #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #endif
Also, don't forget to link to the correct winsock library
Antharax
g++.exe "C:\Dev-Cpp\Work\Socks3\server.cpp" -o "C:\Dev-Cpp\Work\Socks3\server.exe" -I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32" -I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\DOCUME~1\admin\LOCALS~1\Temp/cc2nbaaa.o(.text+0xab):server.cpp: undefined reference to `WSAStartup@8'
C:\DOCUME~1\admin\LOCALS~1\Temp/cc2nbaaa.o(.text+0xf3):server.cpp: undefined reference to `WSACleanup@0'
C:\DOCUME~1\admin\LOCALS~1\Temp/cc2nbaaa.o(.text+0x1dc):server.cpp: undefined reference to `socket@12'
C:\DOCUME~1\admin\LOCALS~1\Temp/cc2nbaaa.o(.text+0x1ed):server.cpp: undefined reference to `WSAGetLastError@0'
C:\DOCUME~1\admin\LOCALS~1\Temp/cc2nbaaa.o(.text+0x22d):server.cpp: undefined reference to `htons@4'
C:\DOCUME~1\admin\LOCALS~1\Temp/cc2nbaaa.o(.text+0x245):server.cpp: undefined reference to `bind@12'
C:\DOCUME~1\admin\LOCALS~1\Temp/cc2nbaaa.o(.text+0x256):server.cpp: undefined reference to `WSAGetLastError@0'
C:\DOCUME~1\admin\LOCALS~1\Temp/cc2nbaaa.o(.text+0x27d):server.cpp: undefined reference to `closesocket@4'
C:\DOCUME~1\admin\LOCALS~1\Temp/cc2nbaaa.o(.text+0x2a0):server.cpp: undefined reference to `gethostname@8'
C:\DOCUME~1\admin\LOCALS~1\Temp/cc2nbaaa.o(.text+0x2b1):server.cpp: undefined reference to `WSAGetLastError@0'
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// with argc the number of elements (C-strings) in argv
while(argc--)
printf("%s\n", *argv++);
exit(EXIT_SUCCESS);
}
//
// Send data to the server
//
char myfile[] = "datafile.dat";
char szBuf[10000]; //<----------------
//char *buffer; //<------does not work
FILE *sourceFile;
if ((sourceFile = fopen(myfile, "r")) == NULL){
printf("Error opening the file\n");
return;
}
//memset(szBuf, 0, sizeof(szBuf));
nRet = fread(szBuf, 1, 10000, sourceFile);
//nRet = fread(buffer, 1, 1000, sourceFile);
//printf("Bytes read from file to szBuf: %d\n", nRet);
fclose(sourceFile);
//Send szBuf to the socket
nRet = send(theSocket,szBuf,strlen(szBuf),0);
closesocket(theSocket);
return;
buffer = malloc(NR_OF_ITEMS * sizeof(char))
while ( (nRet = fread(szBuf, 1, 10000, sourceFile) != 0)
{
send(theSocket,szBuf,nRet,0);
}
fclose(sourceFile);
closesocket(theSocket);
#include <stdio.h>
#include <winsock2.h>
main() definition
{
.
.
.
}
void StreamClient(char *szServer, short nPort)
{.
.
.
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_port = htons(nPort); // Port number from command line
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
.
.
.}
The gethostbyname function cannot resolve IP address strings passed to it. Such a request is treated exactly as if an unknown host name were passed. Use inet_addr to convert an IP address string the string to an actual IP address, then use another function, gethostbyaddr, to obtain the contents of the hostent structure.
Would this theoretically work? I am worrying that there might be some intermediate NULL data sent between packets that can make my server disregard the rest of data.
+-----+-----+-----+
|START| LEN | CRC |
+-----+-----+-----+
+-----------------+
| DATA |
+-----------------+
+-----------+-----+
| DATA CRC | END |
+-----------+-----+
char* ConstructPacket(char *Packet, char *Buf, int szBuf, char *flag)
{
char *newPacket;
newPacket=(char*)calloc(szBuf+sizeof(char), sizeof(char));
strcat(newPacket, flag);
strcat(newPacket, Buf);
free(Packet);
return (newPacket);
}
int szPacket = 512;
int szBuf=sizeof(char)*(szPacket-1); //which is 511
Packet = ConstructPacket(Packet, Buf, szBuf, flag);
//Send Buf to the socket
nSent = send(theSocket,Packet,szPacket,0);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?