Dev-C++ (Bloodshed) - Socket Server

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

62 Replies - 25424 Views - Last Post: 20 December 2012 - 09:43 PM Rate Topic: -----

#1 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Dev-C++ (Bloodshed) - Socket Server

Posted 13 December 2012 - 07:18 PM

I'm having a few difficulties making a socket server via Dev-C++ (Bloodshed).
Throughout this page, i'll also include images to support what I'm doing.
First off,
I was testing out a small application for a very basic "You're connected" code.
So here we go, The code that I currently have:
#include <sdkddkver.h>
#include <conio.h>
#include <stdio.h>

#include <WinSock2.h>
#include <Windows.h>
#include <iostream>

#include <string>

using namespace std;

bool connected = false;

int main() {
	string sentmessage;

	long answer;
	WSAData wsaData;
	WORD DLLVERSION;
	DLLVERSION = MAKEWORD(2,1);
	answer = WSAStartup(DLLVERSION, &wsaData);

	SOCKADDR_IN addr;
	int addrlen = sizeof(addr);

	SOCKET sListen;
	SOCKET sConnect;

	sConnect = socket(AF_INET,SOCK_STREAM,NULL);

	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	addr.sin_family = AF_INET;
	addr.sin_port = htons(1234);

	sListen = socket(AF_INET,SOCK_STREAM,NULL);
	bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
	listen(sListen, SOMAXCONN);

	cout << "Waiting for an incoming conection..." << endl;

	for (;;)/> {
		if (sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen)) {
			if (connected == false) {
				cout << "A connection has been found!";
			    answer = send(sConnect, "YourMessage", 12, NULL);
				connected = true;
			}
			answer = send(sConnect, "Hey", 4, NULL);
		}
	}
}

Looks fine to me!
Nope.
Here are a few errors i'm recieving:
Attached Image
Yes, I do have libws2_32.a linked in paramaters as well:
Attached Image
Not sure what else i'm forgetting to talk about and/or show.
So, just comment if you're trying to help and need more info =D
All help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Dev-C++ (Bloodshed) - Socket Server

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 13 December 2012 - 11:30 PM

Permission Denied

You could change the NULL's for 0 or create a NOFLAG define or constant.
Was This Post Helpful? 1
  • +
  • -

#3 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 14 December 2012 - 04:38 PM

View Post#define, on 13 December 2012 - 11:30 PM, said:

Permission Denied

You could change the NULL's for 0 or create a NOFLAG define or constant.


Alright, I had changed all NULL's to 0's.
Now I get this:
Attached Image
I see you added some link to "Permission Denied" And I did read through it,
But I wasn't exactly sure on what I could fix on my code.
The code remains uneditted from the first post (on this site) other than changing NULL to 0 (All of them)
Any help would continue apprectiated.
And thanks again for your reply =D

EDIT:
The above WAS giving me just that error.
But I tried again (did not change the code, but restarted PC) and now I get no errors,
And no pop-up screen.
This is all that happens (And unable to try to run again):
Attached Image
Was This Post Helpful? 0
  • +
  • -

#4 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 611
  • Joined: 24-July 08

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 14 December 2012 - 07:08 PM

Usually when the compiler gives you a "Permission Denied" error, you have a project in a folder that requires more privileges then what you have, but it appears that your project folder is on your desktop so im not too sure about that one. HOWEVER! if you get that error during run time, then you will need to run your program as administrator.
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 14 December 2012 - 11:30 PM

Does Project1.exe already exist and the compiler cannot delete it?
Was This Post Helpful? 1
  • +
  • -

#6 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 15 December 2012 - 03:15 AM

View PostUG Cyber, on 14 December 2012 - 07:08 PM, said:

Usually when the compiler gives you a "Permission Denied" error, you have a project in a folder that requires more privileges then what you have, but it appears that your project folder is on your desktop so im not too sure about that one. HOWEVER! if you get that error during run time, then you will need to run your program as administrator.

Once the file is ran, it does nothing. No errors, nothing.
The only thing is that I'm unable to try and run again.

Project1.exe is the only file of its kind in my desktop/desktop folders.
Other than that, I'm not sure what's wrong with the code.
Running it as administrator did nothing, either. In fact, either way; there is no pop-up screen of any sort (not even the CMD file for 'cin'/'cout')
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 15 December 2012 - 08:48 PM

1. Is the project a console application?
You could try saving the project in another location.

How do I use A Compiler?

2. Have you tried Rebuild All? Does it display a log?

3. Open a command window. Change directory to your application and run it.
Was This Post Helpful? 0
  • +
  • -

#8 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 16 December 2012 - 04:22 PM

View Post#define, on 15 December 2012 - 08:48 PM, said:

1. Is the project a console application?
You could try saving the project in another location.

How do I use A Compiler?

2. Have you tried Rebuild All? Does it display a log?

3. Open a command window. Change directory to your application and run it.

1. I believe it was originally a Windows Application. I had removed all the default lines that it gave me to start with. I removed them, and started fresh.

2. Here's what I get after 'Rebuild All':
Attached Image

3. I did this, And nothing happened.
It basically just restated what I did. Like this:
"<C:\Users\Name\Desktop>cd C:\Users\Name\Desktop\Test-Server"
"<C:\Users\Name\Desktop\Test-Server>Project1.exe"
"<C:\Users\Name\Desktop\Test-Server>"

And nothing happened.

This post has been edited by Blaze13: 16 December 2012 - 04:24 PM

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6164
  • View blog
  • Posts: 21,251
  • Joined: 05-May 12

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 16 December 2012 - 04:50 PM

Looking at your screenshots, it looks like you are using Windows 7 (or Vista). Did you turn off the default Windows firewall, or at the very least set your firewall settings to allow your program to open a port?

Also, in a post back on Nov 22nd, you said that you weren't going back to Dev C++. What made you change your mind? Dev C++ is quite outdated and you maybe running into issues due to in being so old.
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 16 December 2012 - 05:36 PM

View PostBlaze13, on 17 December 2012 - 01:22 AM, said:

1. I believe it was originally a Windows Application. I had removed all the default lines that it gave me to start with. I removed them, and started fresh.


You should be/are making a console application. With a windows application you would need to create your own window. In a console app you can still use windows.h and its functions such as sockets and messagebox etc.


From : Permission denied while compiling Windows prg

Quote

Heavens, I posted too quickly. I figured out why I was getting a Permission
Denied error; it's because my program was running crashed in the background...
once I killed it in the task manager, I was allowed to compile again. (I
figured it out because I got a few copies of it running and it slowed down my
system so much I was trying to close my programs to shut down and went in to
the task manager to see what was running and there it was.)


So you could try looking at the task manager (Ctrl-Alt-Del).
Was This Post Helpful? 0
  • +
  • -

#11 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 16 December 2012 - 05:51 PM

View PostSkydiver, on 16 December 2012 - 04:50 PM, said:

Looking at your screenshots, it looks like you are using Windows 7 (or Vista). Did you turn off the default Windows firewall, or at the very least set your firewall settings to allow your program to open a port?

Also, in a post back on Nov 22nd, you said that you weren't going back to Dev C++. What made you change your mind? Dev C++ is quite outdated and you maybe running into issues due to in being so old.

It's all fine, currently.

I was having some troubles with VC++. It didn't support the things I needed to run a game I was making (That was in progress). I wanted to go to VC++ because I wasn't haviing troubles making a Socket Server,
But then I tried copy&pasting my program code into it, and I was getting hundreds of errors (on bloodshed, I had none).
So I just stuck to Bloodshed.
Was This Post Helpful? 0
  • +
  • -

#12 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 16 December 2012 - 05:57 PM

View Post#define, on 16 December 2012 - 05:36 PM, said:

View PostBlaze13, on 17 December 2012 - 01:22 AM, said:

1. I believe it was originally a Windows Application. I had removed all the default lines that it gave me to start with. I removed them, and started fresh.


You should be/are making a console application. With a windows application you would need to create your own window. In a console app you can still use windows.h and its functions such as sockets and messagebox etc.


From : Permission denied while compiling Windows prg

Quote

Heavens, I posted too quickly. I figured out why I was getting a Permission
Denied error; it's because my program was running crashed in the background...
once I killed it in the task manager, I was allowed to compile again. (I
figured it out because I got a few copies of it running and it slowed down my
system so much I was trying to close my programs to shut down and went in to
the task manager to see what was running and there it was.)


So you could try looking at the task manager (Ctrl-Alt-Del).

That worked!
I copy&pasted my code into a new Console Application project, and ran it.
The CMD Window then popped up.
However, I am having one more problem I can't work out very well.
Attached Image
As you can see in this photo,
It shows I had found a connection.
Strangely, If you look at the very bottom-right of the image, You'll see i'm not even connected to the internet.
Which means something is wrong with my code.
Here it is, again:
#include <sdkddkver.h>
#include <conio.h>
#include <stdio.h>

#include <WinSock2.h>
#include <Windows.h>
#include <iostream>

#include <string>

using namespace std;

bool connected = false;

int main() {
	string sentmessage;

	long answer;
	WSAData wsaData;
	WORD DLLVERSION;
	DLLVERSION = MAKEWORD(2,1);
	answer = WSAStartup(DLLVERSION, &wsaData);

	SOCKADDR_IN addr;
	int addrlen = sizeof(addr);

	SOCKET sListen;
	SOCKET sConnect;

	sConnect = socket(AF_INET,SOCK_STREAM,0);

	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	addr.sin_family = AF_INET;
	addr.sin_port = htons(1234);

	sListen = socket(AF_INET,SOCK_STREAM,0);
	bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
	listen(sListen, SOMAXCONN);

	cout << "Waiting for an incoming conection..." << endl;

	for (;;)/> {
		if (sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen)) {
			if (connected == false) {
				cout << "A connection has been found!";
			    answer = send(sConnect, "YourMessage", 12, 0);
				connected = true;
			}
		}
	}
}

Not sure where the problem lies, I don't see it anywhere O.o looks fine to me...
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6164
  • View blog
  • Posts: 21,251
  • Joined: 05-May 12

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 16 December 2012 - 06:29 PM

Your check on line 43 assumes that a valid connection has been accepted if the return value is non-zero. According to the documentation, accept() returns INVALID_SOCKET when it fails. Are you sure that INVALID_SOCKET is zero?
Was This Post Helpful? 0
  • +
  • -

#14 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 16 December 2012 - 07:23 PM

No, I'm not sure =o
How do I check it and/or fix it?

This post has been edited by GunnerInc: 18 December 2012 - 06:37 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Dev-C++ (Bloodshed) - Socket Server

Posted 16 December 2012 - 07:36 PM

View PostBlaze13, on 17 December 2012 - 02:57 AM, said:

... I had found a connection.
Strangely, If you look at the very bottom-right of the image, You'll see i'm not even connected to the internet.
Which means something is wrong with my code.
Not sure where the problem lies, I don't see it anywhere O.o looks fine to me...


This is a special address (often called localhost) which doesn't reach out to an external network.

inet_addr("127.0.0.1");



127.0.0.1

This post has been edited by #define: 16 December 2012 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »