?
Solved

C++ Using Winsock to decline connection requests

Posted on 2003-02-23
9
Medium Priority
?
349 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

621 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