• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing a server using socket connection

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what type of code you want??

do you want a code where there is client-server interaction using Sockets?
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
accept is a method of SeverSocket class not of socket class, so following should be your code at server end

 
J radolf
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/***Client Side Program***************/



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


 
J radolf
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code posted by preethi and Swastik is potentially buggy: Don't println to a Socket.
 
J radolf
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about my code sir?
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
january radolf wrote:what about my code sir?

Does it use println in connection with a Socket?
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ulf for correcting the error.
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was there any problem with port number too?
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic