This code snippet demonstrates a simple client/server connection. There is no communication between the client and the server. The purpose of this code is to demonstrate the connection at its simplest. Notice that the server accepts clients in an infinite loop so that it can accept multiple clients.
Client
import java.net.*;
import java.io.*;
class Client{
public static void main(String args[]){
System.out.println( "Connecting to server");
try {
Socket skClient = new Socket( "127.0.0.1", 1234);
System.out.println( "Connected to server!!!");
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
Server
import java.net.*;
import java.io.*;
class Server{
public static void main(String args[]){
try {
ServerSocket skServer = new ServerSocket(1234);
System.out.println( "Server started");
while (true){
Socket skClient = skServer.accept();
}
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
No comments:
Post a Comment