Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-25-2002, 01:45 PM   #1 (permalink)
oops
Registered User
 
Join Date: Jun 2002
Posts: 2
oops is on a distinguished road
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..
oops is offline   Reply With Quote
Old 06-25-2002, 02:48 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-25-2002, 03:09 PM   #3 (permalink)
oops
Registered User
 
Join Date: Jun 2002
Posts: 2
oops is on a distinguished road
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.
oops is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with editing profile cheawick Feedback 0 04-28-2004 10:26 PM
JavaScript Problem dawkim HTML, XML, Javascript, AJAX 2 01-26-2004 07:02 PM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 04:44 PM
This is a windows/C problem UnderWing Standard C, C++ 6 03-28-2003 06:17 AM
Problem printing, please help me laurence10 Java 0 03-11-2003 04:26 PM


All times are GMT -8. The time now is 05:37 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting