stange problem with bind() funcktion - winsock2

S

s_4

Hello!
Sorry about my english - I write to you from poland.
I have strange problem. Bind() return '16'. Documentation say that it
should return 0 (no error) or -1 (some error).

Here is the code of client.

//klient
//stale
#define PORT_UDP 14345
#define PORT_TCP 14344
#define DEF_BLOK 1024
//biblioteki
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <io.h>
using namespace std;

//zmienne i obiekty
int iWinsockStart, iWinsockStop, iConnect, iSend, iRecv, iClose,
iSendTo, iRecvFrom, iUchwytPliku, iOpen, iClosesocketUDP,
iClosesocketTCP, iDane, iSciezka, iOdczytPliku;
int pRozmiarTCP;
char *pSciezkaPliku, *pNazwaPliku;
char *cBuforTCP_wys, *cBuforTCP_odb, *pBuforPliku;
SOCKET sSocketUDP, sSocketTCP;
struct sockaddr_in struktura_UDP, struktura_TCP;

/////////funkcja sprzataj±ca////////////////
int cleanup()
{
cout << "Zamykanie gniazd i sprz±tanie...\n";
iClosesocketTCP = closesocket(sSocketTCP);
iClosesocketUDP = closesocket(sSocketUDP);
iClose = close(iOpen);
iWinsockStop = WSACleanup();
cout << "SocketTCP \t" << iClosesocketTCP << "\n" <<
"SocketUDP \t" << iClosesocketUDP << "\n" <<
"iClose \t" << iClose << "\n" <<
"iWinsockStop \t" << iWinsockStop << "\n";
//zakonczenie programu
system("PAUSE");
return EXIT_SUCCESS;
}

int main()
{
//inicjalizacja zmiennych
pRozmiarTCP = sizeof(struktura_TCP);//rozmiar struktury
memset(&struktura_TCP, 0, sizeof(struktura_TCP)); //zerujemy
strukture
char cBuforUDP_odb[DEF_BLOK] = "";
char cBuforUDP_wys[DEF_BLOK] = "szukam_serwera";
memset(&cBuforUDP_odb, ' ', sizeof(cBuforUDP_odb));


//zaczynamy
WSADATA wsaData;
iWinsockStart = WSAStartup(MAKEWORD(2,2), &wsaData); //Inicjacja sieci
w systemie Windows. Inicjuje u¿ycie biblioteki WS2_32.DLL.
if(iWinsockStart != 0){
printf("Blad funkcji iWinsockStart %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iWinsockStart udana...\n";
//----------------------------------------------------------------
//dane dla gniazda UDP
struktura_UDP.sin_family = AF_INET;
struktura_UDP.sin_port = htons(PORT_UDP);
struktura_UDP.sin_addr.s_addr = /*inet_addr*/
htonl(INADDR_BROADCAST);
memset(&(struktura_UDP.sin_zero), '\0', 8);// wyzeruj resztê
struktury

sSocketUDP = socket(AF_INET, SOCK_DGRAM, 0); //Tworzenie gniazda dla
adresu rozg³oszeniowego
if(sSocketUDP == INVALID_SOCKET){
printf("Blad funkcji sSocketUDP %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja sSocketUDP udana...\n";

setsockopt(sSocketUDP, SOL_SOCKET, SO_BROADCAST, "1", sizeof("1"));

iSendTo = sendto(sSocketUDP, cBuforUDP_wys, DEF_BLOK, 0, (struct
sockaddr *) &struktura_UDP, sizeof(struktura_UDP)); //Wys³anie pakietu
danych do konkretnego adresu.
if(iSendTo == SOCKET_ERROR){
printf("Blad funkcji iSendTo %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iSendTo udana...\n";
//setsockopt(sSocketUDP, SOL_SOCKET, SO_RCVBUF, "1024",
sizeof("1024"));
cout << "Funkcja setsockopt udana...\n";

iRecvFrom = recvfrom(sSocketUDP, cBuforUDP_odb, DEF_BLOK, 0, (struct
sockaddr *) &struktura_TCP, &pRozmiarTCP);
if(iRecvFrom == SOCKET_ERROR){
printf("Blad funkcji iRecvFrom %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iRecvFrom udana...\n";

iClosesocketUDP = closesocket(sSocketUDP);
if(iClosesocketUDP == SOCKET_ERROR){
printf("Blad funkcji iClosesocket %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iClosesocket udana...\n";

//----------------------------------------------------------------
sSocketTCP = socket(AF_INET, SOCK_STREAM, 0);
if(sSocketTCP == INVALID_SOCKET){
printf("Blad funkcji sSocketTCP %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja sSocketTCP udana...\n";
iConnect = connect(sSocketTCP, (struct sockaddr *) &struktura_TCP,
sizeof(struktura_TCP));
if(iConnect == SOCKET_ERROR){
printf("Blad funkcji iConnect %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iConnect udana...\n";
//---------------------- odczyt nazwy pliku ----------------------
cout << "Podaj sciezke do pliku:\n";
cin >> pSciezkaPliku;
//kod pobierajacy ze sciezki pliku, sama jego nazwe
for (int i = 0; i < sizeof(pSciezkaPliku); i++){
if (pSciezkaPliku == '/' || pSciezkaPliku == '\\'){
iSciezka = i;
}
}
for (int i = 0; i < sizeof(pSciezkaPliku); i++){
int j = 0;
if (i > iSciezka){
pNazwaPliku[j] = pSciezkaPliku;
j+=1;
}
}
cout << "Plik ktory wybrales to " << pNazwaPliku << "\n";
//----------------------- wyslanie nazwy pliku -------------------
iSend = send(sSocketTCP, pNazwaPliku, DEF_BLOK, 0);
if(iSend == SOCKET_ERROR){
printf("Blad funkcji iSend1 %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iSend1 udana...\n";
//funkcja open otwiera plik w trybie std::ios_base::binary ze zrodla
sSciezkaPliku
iOpen = open(pSciezkaPliku, ios_base::binary);
if(iOpen == -1){
printf("Blad funkcji iOpen %ld\n", iOpen);
cleanup();
}
else
cout << "Funkcja iOpen udana...\n";
//funkcja read czyta dane z iUchwytPliku do bufora pBuforPliku
//i wielkosci RozmiarBuforaOdczytuZPliku
//------------------- wysys³anie pliku ---------------------------
while (iOdczytPliku = read(iOpen, pBuforPliku, DEF_BLOK)){
if(iOdczytPliku < sizeof(pBuforPliku)){
printf("Blad funkcji iOdczytPliku %ld\n", iOdczytPliku);
cleanup();
}
else
cout << "Funkcja iOdczytPliku udana...\n";
iSend = send(sSocketTCP, pBuforPliku, DEF_BLOK, 0);
if(iSend == SOCKET_ERROR){
printf("Blad funkcji iSend2 %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iSend2 udana...\n";
// iRecv = recv(sSocketTCP, cBuforTCP_odb, sizeof(cBuforTCP_odb),
0);
// &cleanup();
cout << "wyslano pakiet";
}
//------------------- koniec wysylania ---------------------------
iClose = close(iOpen);
if(iClose ==-1){
printf("Blad funkcji iClose %ld\n", iClose);
cleanup();
}
else
cout << "Funkcja iClose udana...\n";
cBuforTCP_wys = "Koniec wysylania";
iSend = send(sSocketTCP, cBuforTCP_wys, DEF_BLOK, 0);
if(iSend == SOCKET_ERROR){
printf("Blad funkcji iSend %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iSend udana...\n";
iRecv = recv(sSocketTCP, cBuforTCP_odb, DEF_BLOK, 0);//Odbieranie
pakietu danych.
if(iRecv == SOCKET_ERROR){
printf("Blad funkcji iRecv %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iRecv udana...\n";
cout << "Serwer odebral" << iDane << "bajtow danych";
cout << "Koncze program";
iClosesocketTCP = closesocket(sSocketTCP); //Zamkniêcie gniazda.
if(iClosesocketTCP == SOCKET_ERROR){
printf("Blad funkcji iClosesocket %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iClosesocket udana...\n";
iWinsockStop = WSACleanup(); //Zakoñczenie pracy z biblioteka
ws2_32.dll.
if(iWinsockStop == SOCKET_ERROR){
printf("Blad funkcji iWinsockStop %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iWinsockStop udana...\n";
return 0;
}
---------------------------------------------------
And here is code of server.

//stale
#define PORT_UDP 14345
#define PORT_TCP 14344
#define DEF_BLOK 1024
//biblioteki
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <io.h>

using namespace std;

//zmienne
int iWinsockStart, iWinsockStop, iSend, iRecv, iSendTo, iRecvFrom,
iClosesocketUDP, iClosesocketTCP, iBind, iListen, iClosesocketACC,
iOdebrano, iOpen, iClose, iWrite, iBind2;
int pRozmiarTCP, *pRozmiarACC;
SOCKET sSocketUDP, sSocketTCP, sAccept, sAccept1;
struct sockaddr_in struktura_UDP, struktura_TCP;
char *cBuforTCP_wys, *cBuforTCP_odb;//cBuforUDP_wys[DEF_BLOK],
cBuforUDP_odb[DEF_BLOK],
char *pNazwaPliku;


//funkcje
void cleanup()
{
cout << "Zamykanie gniazd i sprz±tanie...\n";
//iClosesocketTCP = closesocket(sSocketTCP);
//iClosesocketUDP = closesocket(sSocketUDP);
//iClose = close(iOpen);
//iWinsockStop = WSACleanup();
cout << "SocketTCP \t" << iClosesocketTCP << "\n" <<
"SocketUDP \t" << iClosesocketUDP << "\n" <<
"iClose \t" << iClose << "\n" <<
"iWinsockStop \t" << iWinsockStop << "\n" <<
"iListen \t" << iListen << "\n" <<
"iRecvFrom \t" << iRecvFrom << "\n" <<
"iBind \t" << iBind << "\n" <<
"iSendTo \t" << iSendTo << "\n"
"Rozmiar struktry \t" << sizeof(struktura_UDP) << "\n";
}

int main()
{
//inicjalizacja zmiennych
pRozmiarTCP = sizeof(struktura_TCP);//rozmiar struktury
//memset(&struktura_TCP, 0, sizeof(struktura_TCP)); //zerujemy
strukture
char cBuforUDP_odb[DEF_BLOK] = "";
char cBuforUDP_wys[DEF_BLOK] = "znalazles_serwer";
memset(&cBuforUDP_odb, ' ', sizeof(cBuforUDP_odb));

//zaczynamy
WSADATA wsaData;
iWinsockStart = WSAStartup(MAKEWORD(2,2), &wsaData); //Inicjacja sieci
w systemie Windows. Inicjuje u¿ycie biblioteki WS2_32.DLL.
if(iWinsockStart != 0){
printf("Blad funkcji iWinsockStart %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iWinsockStart udana...\n";
//dane dla gniazda UDP
struktura_UDP.sin_family = AF_INET;
struktura_UDP.sin_port = htons(PORT_UDP);
struktura_UDP.sin_addr.s_addr = /*inet_addr("127.0.0.1");*/
htonl(INADDR_ANY);
memset(&(struktura_UDP.sin_zero), '\0', 8);// wyzeruj resztê
struktury


sSocketUDP = socket(AF_INET, SOCK_DGRAM, 0); //Tworzenie gniazda dla
adresu rozg³oszeniowego
if(sSocketUDP == INVALID_SOCKET){
printf("Blad funkcji sSocketUDP %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja sSocketUDP udana...\n";
setsockopt(sSocketUDP, SOL_SOCKET, SO_BROADCAST, "1", sizeof("1"));

iBind = bind(sSocketUDP, (struct sockaddr *) &struktura_UDP,
sizeof(struktura_UDP)); //Powi±zanie gniazda z adresem/portem (serwer)
if(iBind == SOCKET_ERROR){
printf("Blad funkcji iBind %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iBind udana..." << iBind << "\n";



iRecvFrom = recvfrom(sSocketUDP, cBuforUDP_odb, DEF_BLOK, 0, (struct
sockaddr *) &struktura_TCP, &pRozmiarTCP);//odbieranie danych
datagramowych wraz z przechwyceniem adresu
if(iRecvFrom == SOCKET_ERROR){
printf("Blad funkcji iRecvFrom %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iRecvFrom udana...\n";



iSendTo = sendto(sSocketUDP, cBuforUDP_wys, DEF_BLOK, 0, (struct
sockaddr *) &struktura_TCP, sizeof(struktura_TCP)); //Wys³anie pakietu
danych do konkretnego adresu.
if(iSendTo == SOCKET_ERROR){
printf("Blad funkcji iSendTo %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iSendTo udana...\n";

iClosesocketUDP = closesocket(sSocketUDP);

//ganizdo TCP
sSocketTCP = socket(AF_INET, SOCK_STREAM ,0);
if(sSocketTCP == INVALID_SOCKET){
printf("Blad funkcji sSocketTCP %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja sSocketTCP udana...\n";

//setsockopt(sSocketUDP, SOL_SOCKET, SO_BROADCAST, "1", sizeof("1"));
iBind2 = (sSocketTCP, (struct sockaddr *) &struktura_TCP,
sizeof(struktura_TCP));//Powiazanie gniazda z adresem/portem
WSAGetLastError();
if(iBind2 == SOCKET_ERROR){
printf("Blad funkcji iBind2 %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iBind2 udana..." << iBind2 << "\n";

iListen = listen(sSocketTCP, SOMAXCONN);//nasluchiwanie maksymalnej
liczby placzen
if(iListen == SOCKET_ERROR){
printf("Blad funkcji iListen %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja iListen udana..." << iListen << "\n";

sAccept = accept(sSocketTCP, (struct sockaddr *) &struktura_UDP,
pRozmiarACC);
if(sAccept == INVALID_SOCKET){
printf("Blad funkcji sAccept %ld\n", WSAGetLastError());
cleanup();
}
else
cout << "Funkcja sAccept udana...\n";

//odebranie nazwy pliku
iRecv = recv(sAccept, cBuforTCP_odb, DEF_BLOK, 0);
pNazwaPliku = cBuforTCP_odb;
cout << "Nazwa przesylanego pliktu to " << pNazwaPliku << "\n";

iOpen = open(pNazwaPliku, ios_base::in | ios_base::binary);
do{
iRecv = recv(sAccept, cBuforTCP_odb, DEF_BLOK, 0);
iWrite = write(iOpen, cBuforTCP_odb, DEF_BLOK);
iOdebrano += iRecv;
cout << "odebrano " << iOdebrano << " bajtow\n";
}
while(iRecv>0);
iClose = close(iOpen);

cout << "Game_Over\n";
iClosesocketUDP = closesocket(sSocketUDP);
iClosesocketTCP = closesocket(sSocketTCP);
iClosesocketACC = closesocket(sAccept);
iWinsockStop = WSACleanup();
system("PAUSE");
return EXIT_SUCCESS;
}

Please help!
I seting on it from few hours and I cant handle it...
One more thing. Please do not use informal language. I may not
understand it.
Thanks in advance.
 
J

Jens Thoms Toerring

Sorry about my english - I write to you from poland.
I have strange problem. Bind() return '16'. Documentation say that it
should return 0 (no error) or -1 (some error).

<some Windows specific C++ code snipped>

Sorry, but not only is your code totally system specific, so you
should ask in some Windows newsgroup, moreover it's not even C but
obviously C++. But what we're discussing here is the language C,
not C++ (there's comp.lang.c++ for that, but you should go to a
Windows specific group anyway since your problem doesn't seem to
be about C++ but about some system specific extensions to C++).

Regards, Jens
 
R

Richard Heathfield

(e-mail address removed) said:
Hello!
Sorry about my english - I write to you from poland.
I have strange problem. Bind() return '16'. Documentation say that it
should return 0 (no error) or -1 (some error).

The best thing you could do is to ask this question in
comp.os.ms-windows.programmer.win32

Lepszy zadawac w comp.os.ms-windows.programmer.win32 - prosze wybaczac
moj kiepski polski. Szczescie!
 
S

santosh

Hello!
Sorry about my english - I write to you from poland.
I have strange problem. Bind() return '16'. Documentation say that it
should return 0 (no error) or -1 (some error).

Here is the code of client.

Please help!
I seting on it from few hours and I cant handle it...
One more thing. Please do not use informal language. I may not
understand it.
Thanks in advance.

In addition to what the others have said, try to derive a minimal
compilable subset of your code that still exhibits your problem. As it
stands, your code is a bit too long for many posters to bother
verifying.
 
S

Serve Laurijssen

Jens Thoms Toerring said:
<some Windows specific C++ code snipped>

Sorry, but not only is your code totally system specific, so you
should ask in some Windows newsgroup, moreover it's not even C but
obviously C++. But what we're discussing here is the language C,
not C++ (there's comp.lang.c++ for that, but you should go to a
Windows specific group anyway since your problem doesn't seem to
be about C++ but about some system specific extensions to C++).

its more C style code than C++ tbh
strange mixture of using printf and cout, try to stick to one at least!
 
S

s_4

It should be in c++ but I have to write with winsock2, and in winsock
documentation there is meny things from c. Main socket funcktions are
from c so Im asking here. I have never programing in c and I have
programing in c++ for abuot 1 year. So I dont know exactly what is the
diference between c and c++. Many functions are simlar, I think or
even they are rewrite from c to c++.
Thank you for answer on my problem.

PS. Its write in Dev-cpp compilator so if you want to compile it in
dev-cpp you should add two librarys libwsock.a and libws2_32.a in
project options...

Thank you once again.
 
F

Flash Gordon

It should be in c++ but I have to write with winsock2, and in winsock
documentation there is meny things from c. Main socket funcktions are
from c so Im asking here. I have never programing in c and I have

They are not from C they are from Windows. So, as you were told, you
need to ask in a Windows group of some sort.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top