Consultor Eletrônico



Kbase P131422: How to test TCP communication with Java
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   12/07/2010
Status: Verified

GOAL:

How to test TCP communication with Java


GOAL:

Java TCP sample test programs


FACT(s) (Environment):

Progress 9.x
OpenEdge 10.x
UNIX

FIX:

1) Copy the test programs TstTCP_S.java (Server) and TstTCP_C.java (client) in your working directory.
2) Set the environment to compile and run the programs like this:

a.- Run

$DLC/proenv
b.- Run

. $DLC/bin/slib_env
c.- Run

. $DLC/bin/java_env
d.- Compile Java programs

javac *.java
e.- Run server program in background mode

java TstTCP_S &
f.- Run client program

java TstTCP_C
Once you run the server as background and then the client you will get a message from the server.



Example
proenv> java TstTCP_C

Server: Server ready, echoing input from client, to finish type: bye
abc
Client: abc
Server: abc
bye
Client: bye
Server: bye
Client:
[1]+ Done java TstTCP_S



__________________ SERVER PROGRAM __________________

/** ----------------------------- */
/** Program TstTCP_S.java */
/** ----------------------------- */
import java.net.*;
import java.io.*;
public class TstTCP_S {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
&nbsp.; clientSocket.getInputStream()));
String inputLine, outputLine;
outputLine = "Server ready, echoing input from client, to finish type: bye";
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = inputLine;
out.println(outputLine);
if (outputLine.equals("bye"))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}

__________________ CLIENT PROGRAM __________________

/** ----------------------------- */
/** Program TstTCP_C.java */
/** ----------------------------- */
import java.io.*;
import java.net.*;
public class TstTCP_C {
public static void main(String[] args) throws IOException {
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
kkSocket = new Socket("localhost", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;

fromUser = stdIn.readLine();
&nb.sp; if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
}
}.