aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes accessing a server using socket connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "accessing a server using socket connection" Watch "accessing a server using socket connection" New topic
Author

accessing a server using socket connection

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,
I am very new to socket.I need to access a server using socket connection.Is it possible to do this?if so please give me a hint of code.

Thanks
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
what type of code you want??

do you want a code where there is client-server interaction using Sockets?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Very much possible, at the Server side you use ServerSocket to crate a socket on particular port on which server will accept the request, you now create a Socket from this and wrap your IO streams to this.




//Client creates a socket specifying the ip address and port number of server
Socket socket=new Socket("127.0.0.1",1000);
and obtains the IOStreams in the same way.


Swastik
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks.I have tried the following sample:



But i am getting the following error:

The method accept() is undefined for the type Socket
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

accept is a method of SeverSocket class not of socket class, so following should be your code at server end

J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
/***Client Side Program***************/



/**** Server Side Program****************/


J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
Ya as Swastik said Its a method of ServerSocket class.

The code snippet i provided should work just try it once and post your queries here if you got any problems.


preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
But i didnt get any output if i run this program.couldn't i mention the ip address with the port?
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
Probably you should mention the IP address in the client side and mention only the port number in the server side using the ServerSocket class.

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

this is a simple client code



First execute the server code and from a different console execute the client code. The server console should display a method like Hi server and client console should display a message Welcome to Socket Programming.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
swastik, I have tried your code.when running the server code,I am getting the following message:

java.net.BindException: Address already in use: JVM_Bind


And when running the client code,the output is:

Asterisk Call Manager/1.0


And can i run both the codes in eclipse in same system?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

preethi
As from the error it seems that some other service is already running on that mentioned port, so just change the port number in the ServerSocket constructor, and accordingly make changes in the client code as well i.e. the port number, and make sure that server starts properly, then only client will run.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

You should be able to run it in the same system, so either put the exact IP of the system in the client code, or use 127.0.0.1 i.e. the loopback address.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
The code posted by preethi and Swastik is potentially buggy: Don't println to a Socket.


Ping & DNS - my free Android networking tools app
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
what about my code sir?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Thanks, the information is really very useful, however while executing codes haven't faced any issues. But using print instead of println will of course optimize the code.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
january radolf wrote:what about my code sir?

Does it use println in connection with a Socket?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks ulf for correcting the error.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Was there any problem with port number too?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

All your codes help me to understand about sockets.I need to access the asterisk's commands using java socket.Is it possible to do this?

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
I need to access the asterisk's commands using java socket.Is it possible to do this?

I would assume so; check the Asterisk documentation for details.
 
jQuery in Action, 2nd edition
 
subject: accessing a server using socket connection