Click here to Skip to main content
15,900,110 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: OnInitDialog() direct call Pin
Michael P Butler28-Dec-02 1:02
Michael P Butler28-Dec-02 1:02 
GeneralRe: OnInitDialog() direct call Pin
Brian Delahunty28-Dec-02 1:19
Brian Delahunty28-Dec-02 1:19 
GeneralRe: OnInitDialog() direct call Pin
alex.barylski28-Dec-02 15:19
alex.barylski28-Dec-02 15:19 
GeneralResizing of CFormView based window Pin
M.Shoaib Khan27-Dec-02 21:03
sussM.Shoaib Khan27-Dec-02 21:03 
GeneralRe: Resizing of CFormView based window Pin
alex.barylski27-Dec-02 22:01
alex.barylski27-Dec-02 22:01 
GeneralFTP question Pin
focusphs27-Dec-02 19:09
focusphs27-Dec-02 19:09 
GeneralRe: FTP question Pin
User 665828-Dec-02 1:55
User 665828-Dec-02 1:55 
GeneralURG:: CloseSocket() error with Multithreading. Pin
ydasari27-Dec-02 15:24
ydasari27-Dec-02 15:24 
Hi All

I appreciate greatly a quick response. Thank you.


I am using unicast sockets (sync) where i bind the socket in one thread and keep accept() new calls in another thread. and Closesocket() in another.

When I closesocket(), the program terminates. and if i remove closesocket(), it does not allow me to bind() again.

I forgot to mention that I need to close and connect again without terminating the application (reuse the socket). Is there any way to do this or what am i doing wrong?

int UnicastConnect()
{
printf("Establishing the listener...");
WORD version = MAKEWORD(1,1);
WSADATA wsaData;

WSAStartup(version, &wsaData);

// Next, create the socket itself
listeningSocket = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);

if (listeningSocket == INVALID_SOCKET) {
AfxMessageBox("Error at socket()");
WSACleanup();
return 0;
}

// Use SOCKADDR_IN to fill in address information
SOCKADDR_IN saServer;

saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY; saServer.sin_port = htons(g_InetPort);

/*
setsockopt( listeningSocket, SOL_SOCKET, SO_REUSEADDR, NULL, NULL );
*/

// Bind the socket to our local server address
nRet = bind(listeningSocket, (LPSOCKADDR)&saServer, sizeof(struct sockaddr));
if (nRet == SOCKET_ERROR) {
AfxMessageBox("Error Binding Socket");
WSACleanup();
return 0;
}

// Make the socket listen
nRet = listen(listeningSocket, MAX_CLIENTS);
if (nRet == SOCKET_ERROR) {
WSACleanup();
return 0;
}

if (listeningSocket == INVALID_SOCKET) {
return false;
}

CreateThread(0, 0, AcceptConnections, (void *)
listeningSocket, 0, NULL);

}

DWORD WINAPI AcceptConnections(void *ListeningSocket)
{
sockaddr_in sinRemote;
int nAddrSize = sizeof(sinRemote);

while (1) {
SOCKET sd = accept((SOCKET) ListeningSocket, (sockaddr*)&sinRemote,&nAddrSize);

*p = 1 ;
ioctlsocket(sd, FIONBIO, p); // Sets the Non-Blocking Mode of Socket.

if (sd != INVALID_SOCKET) {
AfxMessageBox ("BeaconSender::Accepted Unicast connection from ");
}
else {
AfxMessageBox ("Closed");
return false;
}
}
}

void stop(){
shutdownsocket(listeningsocket)
closesocket(listeningsocket); //Gives an error
wsacleanup();
}


Thanks for your help.
Yogi


GeneralRe: URG:: CloseSocket() error with Multithreading. Pin
valikac27-Dec-02 16:07
valikac27-Dec-02 16:07 
GeneralRe: URG:: CloseSocket() error with Multithreading. Pin
Anonymous28-Dec-02 4:56
Anonymous28-Dec-02 4:56 
GeneralCreateFile and Long Path/Files :: Win32 Pin
valikac27-Dec-02 14:35
valikac27-Dec-02 14:35 
GeneralRe: CreateFile and Long Path/Files :: Win32 Pin
Todd Smith27-Dec-02 14:50
Todd Smith27-Dec-02 14:50 
GeneralRe: CreateFile and Long Path/Files :: Win32 Pin
Michael Dunn27-Dec-02 14:50
sitebuilderMichael Dunn27-Dec-02 14:50 
GeneralRe: CreateFile and Long Path/Files :: Win32 Pin
valikac27-Dec-02 14:58
valikac27-Dec-02 14:58 
GeneralRe: CreateFile and Long Path/Files :: Win32 Pin
carpediem27-Dec-02 16:56
carpediem27-Dec-02 16:56 
GeneralRe: CreateFile and Long Path/Files :: Win32 Pin
valikac27-Dec-02 17:27
valikac27-Dec-02 17:27 
GeneralRe: CreateFile and Long Path/Files :: Win32 Pin
Michael Dunn27-Dec-02 19:26
sitebuilderMichael Dunn27-Dec-02 19:26 
GeneralRe: CreateFile and Long Path/Files :: Win32 Pin
valikac27-Dec-02 20:47
valikac27-Dec-02 20:47 
GeneralSDI Window in a MFC DLL Pin
Datalord3227-Dec-02 10:35
sussDatalord3227-Dec-02 10:35 
GeneralAdding formatted line of text to CRichEditCtrl Pin
IGx8927-Dec-02 10:21
IGx8927-Dec-02 10:21 
GeneralRe: Adding formatted line of text to CRichEditCtrl Pin
IGx8927-Dec-02 16:05
IGx8927-Dec-02 16:05 
Generali love this site ! Pin
HellShrimp4free27-Dec-02 10:17
HellShrimp4free27-Dec-02 10:17 
GeneralRe: i love this site ! Pin
Hockey27-Dec-02 11:40
Hockey27-Dec-02 11:40 
GeneralRe: i love this site ! Pin
Nitron28-Dec-02 5:06
Nitron28-Dec-02 5:06 
Generalstrtok - reconstruct string after it finds a token Pin
jimNLX27-Dec-02 10:15
jimNLX27-Dec-02 10:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.