File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Socket Accept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket Accept" Watch "Socket Accept" New topic
Author

Socket Accept

Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
We all know that socket.accept(); means accept the connection if its requested

but does anyone one how to do the following

if ( Connection request is received)
{
dont accept immediately and ask the user.

}


i am searching for the method but i didnt find it can anybody help me with this please
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

What "user" do you want to ask? Is it somebody sitting next to the server waiting to approve connections? Do you have to send a message somewhere and receive a response? Is the "user" the client who is trying to connect, and if so what are you going to ask?
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
i want to send a file across the network


its like : do you want to accept this file Yes or no


so that's why i am asking how to monitor Socket connection requests

is there a way
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

Still don't understand. This is the server code you are asking about, right? So who is the "you" in your question?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

I think I might understand the question now. It isn't about asking the "user" at all. I think the question is, how do you listen for a socket connection without using the accept() method.

Why would you want to do that? If you don't call accept() then you don't know anything at all about the connection which is about to be made. So what are you going to do? Ask the "user" if they want to accept this connection about which there is no information at all?

Why not accept the connection, then decide if you want to allow it to proceed? You can always close it whenever you like. Just because the method is named "accept" that doesn't mean you are forced to write code which processes any request that comes over the connection.
Peter Lawrey
Ranch Hand

Joined: Dec 21, 2008
Posts: 62
When you accept a Socket connect all you get is the Socket.
Typically, the client send something to the server and only then does the server decide if what the client requests is allowed.
If not, the server can send a response and close the socket.

If you want to restrict which clients can access your server you can install a firewall which can allow clients from limited IP addresses.
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
Paul i wanna know about the incoming connection so that i will choose between deny or accepting

if i dont know about the connection than how will i be to know if someone is sending me a file.

what peter said is what i want and i could have done it this way

but its much easier to know about the incoming connection cause if the server didnt accept the connection the client will be forced to wait without needing to notify him and tell him to initiate the transfer
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

No, Peter is correct. You accept the connection and then decide what to do with it. Of course the client will be forced to wait until you accept it, but once you do then you can process the transfer. Or not, if that's what you want to do. I guess I still don't understand the question in the background, though. What is stopping you from doing it the same way that every other server in the world already does it?
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
if i accepted the connection the transfer will proceed cause there is no waiting statement that tells the sender to wait!!!

thats what am having hard time with...


i just want to detect connnection request

cause if i accepted the connection i need than to implement a handshake process between the 2 sides which will be big chunks of code which i dont want
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

Would it be fair to say that you have never seen a real socket-based application? From what you're saying, it certainly seems that way. How about if you have a look at the Sockets tutorial?
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
Mate you are killing me over here

i can do all of this but i am trying to find another way

i already mentioned in my post that it can be done other way but i just wanna know if there is a method to detect connection requests

i consider this as an insult Please edit your post
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

Well, I am sorry if you thought that was an insult. I apologize for that.

So you want the short answer? No, the only way to find out about a connection is to accept it. I can't see what your problem is with doing that, though. Just accept it and deal with it. If you want to deny it, then just close the connection.
Mohamad Obagi
Ranch Hand

Joined: Oct 25, 2008
Posts: 37
I fixed it in my way at least

used two different ports one for listening to acknowledgment
and one for transfering.

cause i really was having troubles on how to make the other thread wait
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket Accept