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
posted
0
Thanks.I have tried the following sample:
But i am getting the following error:
The method accept() is undefined for the type Socket
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
posted
0
swastik, I have tried your code.when running the server code,I am getting the following message:
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.
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.
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.