|
 |
|
 |
06-25-2002, 01:45 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Jun 2002
Posts: 2
|
Winsock problem..
Any idea whats wrong with this code:
Code:
#include <windows.h>
#include <winsock.h>
#include <string.h>
#include <stdlib.h>
#include "resource.h"
#define MESSAGE "Hello, Client -!\n" //Connection Message
//Global varables to store port numbers
char portNum[6];
int port;
BOOL MainDialog_OnCommand(HWND, WORD, WORD, HWND);
BOOL MainDialogProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)MainDialogProc, 0);
return 0;
}
BOOL MainDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
return MainDialog_OnCommand(hWnd, LOWORD(wParam),HIWORD(wParam), (HWND)lParam);
case WM_CLOSE:
EndDialog(hWnd, 0);
return TRUE;
}
return FALSE;
}
BOOL MainDialog_OnCommand(HWND hWnd, WORD wCommand, WORD wNotify, HWND hControl)
{
switch (wCommand)
{
case IDC_BUTTON1:
GetDlgItemText(hWnd, IDC_EDIT1, portNum, 6);
port = atoi ((char *) &portNum);
WSADATA wsda; //Structure to restore info returned from WSAStartup()
char szRepMessage[80]; //Place to hold the reply message
int iMessageLen;
int ret; //Return values
char szInBuffer[128];
int iBufferLen;
int iPort, iAddrLen;
iPort = port;
SOCKET sListen, sClient; //TCP socket Handle
SOCKADDR_IN addr, //Local interface
remote_addr; //Address of the connecting host
if(iPort < 0 || iPort> 65535)
{
MessageBox(NULL, "Port Range: 0 - 65535", "Error", MB_OK);
break;
}
strcpy(szRepMessage, MESSAGE);
iMessageLen = strlen(szRepMessage);
WSAStartup(MAKEWORD(1,1), &wsda); //Load winsock 1.1
SetDlgItemText(hWnd, IDC_EDIT1, "Creating socket...\n");
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); //Create Socket
//Check for error
if(sListen == SOCKET_ERROR)
{
SetDlgItemText(hWnd, IDC_EDIT1, "Call To socket failed with some ****\n");
break;
}
SetDlgItemText(hWnd, IDC_EDIT1, "OK\n");
SetDlgItemText(hWnd, IDC_EDIT1, "Binding socket to X...\n");
addr.sin_family = AF_INET;
addr.sin_port = htons(iPort);
addr.sin_addr.s_addr = htonl(INADDR_ANY); //Listen on any available interface
ret = bind(sListen, (struct sockaddr *) &addr, sizeof(addr));
//Check for another error
if(ret == SOCKET_ERROR)
{
SetDlgItemText(hWnd, IDC_EDIT1, "Call to bind socket ****ed up :(\n");
break;
}
SetDlgItemText(hWnd, IDC_EDIT1, "OK\n");
SetDlgItemText(hWnd, IDC_EDIT1, "Putting socket into listening mode...\n");
ret = listen(sListen, 10); //Backlog 10
//Check for another error
if(ret == SOCKET_ERROR)
{
SetDlgItemText(hWnd, IDC_EDIT1, "Call to listen failed...\n");
break;
}
SetDlgItemText(hWnd, IDC_EDIT1, "OK\n");
SetDlgItemText(hWnd, IDC_EDIT1, "Waiting for Connections...\n");
iAddrLen = sizeof(remote_addr);
sClient = accept(sListen, (struct sockaddr *) &remote_addr, &iAddrLen);
//Check for errors (again)
if(sClient == SOCKET_ERROR)
{
SetDlgItemText(hWnd, IDC_EDIT1, "Call to accept failed...\n");
break;
}
SetDlgItemText(hWnd, IDC_EDIT1, "Connected...\n");
//Receive Data
SetDlgItemText(hWnd, IDC_EDIT1, "Waiting for a response...\n");
ret = recv(sClient, szInBuffer, sizeof(szInBuffer), 0);
if(ret == SOCKET_ERROR)
{
SetDlgItemText(hWnd, IDC_EDIT1, "Call to recv failed...\n");
break;
}
SetDlgItemText(hWnd, IDC_EDIT1, "Response received...\n");
iBufferLen = ret; //recv() returns the number of bytes read
szInBuffer[iBufferLen] = '\0'; //Convert to CString
SetDlgItemText(hWnd, IDC_EDIT1, "Data received...\n");
//Ready to send data
SetDlgItemText(hWnd, IDC_EDIT1, "Sending reply...\n");
ret = send(sClient, szRepMessage, iMessageLen, 0);
//Check for yet another ****ing error
if(ret == SOCKET_ERROR)
{
SetDlgItemText(hWnd, IDC_EDIT1, "Call to send failed...\n");
break;
}
SetDlgItemText(hWnd, IDC_EDIT1, "OK\n");
SetDlgItemText(hWnd, IDC_EDIT1, "Closing socket...\n");
closesocket(sClient);
SetDlgItemText(hWnd, IDC_EDIT1, "Closed...\n");
return 0;
break;
case IDCANCEL:
EndDialog(hWnd, wCommand);
break;
}
return TRUE;
}
For some reason, this code hangs when i try to run the compiled program..No errors pop up, the program just hangs..Just wondering if you guys can spot anything wrong with ym code..
|
|
|
06-25-2002, 02:48 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
|
What do you mean, it just hangs ?
You're not getting any messages or anything from it ?
Or you are getting messages, but it never comes past the accept(...) part ?
You do know, befor you can go on from that line, there has to be a client connecting on the port, this is listening from. (addr.sin_port) Else accept() will never return.
|
|
|
06-25-2002, 03:09 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Jun 2002
Posts: 2
|
No worries man..I got it fixed..Damn Backlog was making it hang..
YEah, the accept is fine..I have a client i wrote continuously making connection attempts in a loop 
Thanks for the help.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:37 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|