?
Solved

C++ Using Winsock to decline connection requests

Posted on 2003-02-23
9
Medium Priority
?
343 Views
Last Modified: 2013-11-13
How do I use Winsock to decline connection requests from a different computer? (i mean to block it off)

[if you have an answer, please explain it detailed, and fully, for i am new to this topic] (C++)
0
Comment
Question by:neohacker2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 2

Expert Comment

by:muskad202
ID: 8003945
hi.
u have not mentioned what language u are using, ..
assuming it is vb..

in the connection request event handler, the connection is only accepted when u do a winsock1.accept call. if u don't want to accept the connection, then u don't need to make that call.

the same thing in c, after u do a listen call on the socket, the connection is only accepted if u do an accept call. wht u can do is, do accept, then immediately call close the connection using closesocket, and then do the whole thing again, (i.e the listen also)
0
 
LVL 5

Expert Comment

by:msa2003
ID: 8010800
This is a part of an existing (and working) multithreaded web-server source:

struct sockaddr_in {
        short   sin_family;
        u_short sin_port;
        struct  in_addr sin_addr;
        char    sin_zero[8];
};

// ..... S is a listening socket

sockaddr_in Addr;
int Size = sizeof(Addr);
SOCKET NewSocket = accept(S, &Addr, &Size);
if (/* some condition */) {
  // accept socket
} else {
  // decline socket
  shutdown(NewSocket, SD_BOTH);
  closesocket(NewSocket);
}
0
 

Author Comment

by:neohacker2
ID: 8011838
No I mean, to decline any connection requests, no "if"s to accept them. I want to create something like a firewall to block every single connection from another computer.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:kelly_mrt
ID: 8011971
Rather than block every single connection, why don't you close the port?  If you must keep the port open, just don't 'Listen' on it - then you won't accept any connections.
0
 
LVL 5

Expert Comment

by:msa2003
ID: 8018033
It's strange enough...
If you wont exactly to decline any connection, just exclude "if":

sockaddr_in Addr;
int Size = sizeof(Addr);
SOCKET NewSocket = accept(S, &Addr, &Size);
 // decline socket
shutdown(NewSocket, SD_BOTH);
closesocket(NewSocket);

I just do not understand how it could help you make firewall... If the port in your computer is not listening, it will never accept any incoming connect. If it is already listening, you could not block it using WinSock.

Firewall may be a type of proxy or a router with packet filtering.
0
 

Author Comment

by:neohacker2
ID: 8018877
Well, yeah, i did take the "if" out, but it still doesn't work. it says a lot of errors. i tried fixing it and got here:


#include <winsock.h>


SOCKET SD_BOTH;
SOCKET server;


void main()
{
sockaddr_in Addr;
int Size = sizeof(Addr);
SOCKET NewSocket = accept(server, &Addr, &Size);
shutdown(NewSocket, SD_BOTH);
 closesocket(NewSocket);
                    };
}

Here is the error:


ONEBLOCK.cpp
C:\Documents and Settings\Administrator\¹ÙÅÁ È­¸é\security\ZONEBLOCK.cpp(18) : warning C4551: function call missing argument list
C:\Documents and Settings\Administrator\¹ÙÅÁ È­¸é\security\ZONEBLOCK.cpp(24) : error C2664: 'accept' : cannot convert parameter 2 from 'struct sockaddr_in *' to 'struct sockaddr *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.


Plz i really need this help.
0
 

Author Comment

by:neohacker2
ID: 8018932
oops there are a few program mistypes there, sorry.
0
 
LVL 5

Accepted Solution

by:
msa2003 earned 300 total points
ID: 8020095
Try the following:

sockaddr_in Addr;
int Size = sizeof(Addr);
SOCKET NewSocket = accept(S, &(sockaddr)Addr, &Size);
// decline socket
shutdown(NewSocket, SD_BOTH);
closesocket(NewSocket);
0
 

Author Comment

by:neohacker2
ID: 8021274
Thanks, I really needed it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
This article will show, step by step, how to integrate R code into a R Sweave document
This video teaches viewers about errors in exception handling.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question