im wondering if there are two programs, one called server
and the other called client
and these two are illustrating a server and client respectively, is it possible to test them in one machine? considering i defined the client socket as follow:
socket = new Socket("127.0.0.1",3000);
Edit: Server part:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
class TCPServer
{
private static ServerSocket serverSocket;
private static Socket socket;
private static PrintWriter toClient;
private static BufferedReader fromClient;
public static void run() throws IOException
{
System.out.println("Server is waiting for connections...");
while (true)
{
openStreams();
processClient();
closeStreams();
}
}
public static void openStreams() throws IOException
{
serverSocket = new ServerSocket(3000);
socket = serverSocket.accept();
toClient = new PrintWriter(socket.getOutputStream(),true);
fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public static void closeStreams() throws IOException
{
fromClient.close();
toClient.close();
socket.close();
serverSocket.close();
}
public static void processClient()throws IOException
{
String msg = null;
do {
msg = fromClient.readLine();
toClient.println("Client said " + msg);
} while (!"exit".equals(msg));
}
}
client part :
package clientserver.pkg1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
class TCPClient
{
private PrintWriter toServer;
private BufferedReader fromServer;
private Socket socket;
public void Client( )throws IOException
{
socket = new Socket("127.0.0.1",3000);
}
public void openStreams() throws IOException
{
toServer = new PrintWriter(socket.getOutputStream(),true);
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void closeStreams() throws IOException
{
fromServer.close();
toServer.close();
socket.close();
}
public void run()throws IOException
{
openStreams();
String msg = "";
Scanner scanner = new Scanner(System.in);
while (!"exit".equals(msg))
{
System.out.print(">");
msg = scanner.nextLine();
toServer.println(msg);
String tmp = fromServer.readLine();
System.out.println("Server said: " + tmp);
}
closeStreams();
}
}
main class
public static void main(String[] args) {
try
{
TCPServer server = new TCPServer() {};
TCPClient client = new TCPClient();
client.run();
server.run();
}
catch(IOException e)
{
System.err.println("Error in Client " + e.getMessage());
}
java.lang.NullPointerException
1
toServer = new PrintWriter(socket.getOutputStream(),true);
2
public void run()throws IOException
{
openStreams();
.....
}
3
public static void main(String[] args) {
try
{
TCPClient client = new TCPClient();
client.run();
}
at client.run();
You certainly can, just have the client and server connected to the same port number.
Yes, you can write such a program. There is a useful tutorial for this. In short, it all boils down to prepare a ServerSocket:
From the programmer's point of view, there is no difference between a server residing on a remote machine or on a local one.
You could try writing two different programs, one for the server and one for the client. This is the main for the client:
And this for the server:
Then run the server first and then the client in a different console.
sure, you can test them in the same machine,. years ago i made the chatting-like application using socket, and i tested it to the same machine before i did that with some different machine, and that was go fine,.
you can read/write on a port of your PC, independant from where in the network you are