|
-
How to create socket connection in Java
-how to create connections for P2P system.
-how to transfer file from 1 peer to another peer.
-
Reef Shark
i think u will need a client and a server. A server will have to listen to a particular port and accept any incoming connections from the client. The client basically has to know the IP address of the server and the port number the server is listening to.
If you want to be able to connect both ways then u will need client/server functionality on both ends.
Here is sample Server:
class Server
{
public static void main(String[] args)
{
PortListener listener = new PortListener();
listener.start();
}
}
public class PortListener extends Thread
{
private ServerSocket _socket;
public PortListener()
{
try
{
_socket = new ServerSocket(1000);
}
catch (Exception e)
{
System.exit(-1);
}
}
public void run()
{
while(true)
{
try
{
// waiting for the next client connection to be established
Socket clientSocket = _socket.accept();
ClientInterface currConn = new ClientInterface(clientSocket);
currConn.start();
}
catch (Exception e)
{
}
}
}
}
The ClientInterface is another thread that establishes a dedicated connection to client and reads whatever is coming in through the socket. Reading and writing is done using streams.
to implement a client have a look at Socket class. If u need more code let me know.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|