473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

stange problem with bind() funcktion - winsock2

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[i] == '/' || pSciezkaPliku[i] == '\\'){
iSciezka = i;
}
}
for (int i = 0; i < sizeof(pSciezkaPliku); i++){
int j = 0;
if (i iSciezka){
pNazwaPliku[j] = pSciezkaPliku[i];
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.

Feb 17 '07 #1
6 2119
s_*@wp.pl wrote:
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Feb 17 '07 #2
s_*@wp.pl 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!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 17 '07 #3
s_*@wp.pl wrote:
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.
<snip ~400 lines of C++>
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.

Feb 17 '07 #4

"Jens Thoms Toerring" <jt@toerring.dewrote in message
news:53*************@mid.uni-berlin.de...
s_*@wp.pl wrote:
>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++).
its more C style code than C++ tbh
strange mixture of using printf and cout, try to stick to one at least!
Feb 17 '07 #5
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.

Feb 17 '07 #6
s_*@wp.pl wrote, On 17/02/07 22:14:
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.
--
Flash Gordon
Feb 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Holger Fleckenstein | last post by:
Hi, I have a problem with a server socket (winsock2.h). I do all standard stuff like listen(), accept() and so on, which all works fine. I only want one client to be able to connect at a time and...
1
by: Adam Teasdale Hartshorne | last post by:
Using the code below I get a very stange result. I assume that I must be doing something wrong, but I can't figure out what it is. getTriangleEdges(t,t1e) ; for (int i = 0 ; i <...
10
by: Ka | last post by:
Hi, all I use Redhat8.0, and I complie a program with wine in this box, my wine version is wine-20020605-2 by Redhat. and after install wine, I just locate the library in /usr/lib/wine ...
11
by: Sweety | last post by:
hello to all members, i have strange question in C. main() { printf("%d",main) ; } here o/p is same for all m/c in TC++ version 3.0 i.e 657. I think this is not garbage.
2
by: Steffo | last post by:
Hi I seem to experience a problem when including "Winsock2.h" in my Windows Forms application. I can't insert any images to an imagelist. I get the following error: : error C2039: 'GetObjectA' :...
0
by: integragreg | last post by:
I apologize in advance if I am posting to the wrong group, but at least one of my questions is related to Platform Invoke in C#. I am using .NET Framework 1.1, and for improved performance, I...
0
by: rodrado | last post by:
I recently began getting this error when SQL Server 2000 starts up. I tried all the suggestions I could find (e.g., Q293107) but none worked. I did finally figure this out and wanted to share the...
6
by: ghssal | last post by:
HI how can run PL/SQL application as: create or replace function getChecksiffra (in_string varchar2) return number is xSvar number := 0; xNR number := 0; xSumma number := 0; ...
4
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.