Na empresa a qual trabalho tem um servidor nosso que roda java applet ele tem uma pasta Client onde as classes devem ficar e uma pasta Public onde o html e a class applet devem ficar.
Ateh ai tudo bem porém, eu preciso criar um socket para comunicar com o servidor e o usuario para enviar as mensagens nao sei porque nenhum exemplo que eu ja peguei deram certo.
Gostaria da ajuda de vcs. Nesse servidor ja venho um código teste o qual faz a conexao apenas uma vez e depois que eu saiu e volto nao funciona mais vou posta o exemplo.
import OAJUtil.*;
import java.io.*;
import java.net.*;
public class Client implements JatConstants
{
static ServerSocket serverSocket = null;
static Socket clientSocket = null;
public static PrintWriter out = null;
class ipClient extends Thread
{
private static final int MAXRCVLENGTH = 512;
BufferedReader in = null;
private String parseString(String rcvbuf, int offset)
{
String retval = "";
int i = offset;
while ((rcvbuf.charAt(i) != '<') && (i < rcvbuf.length())) {
retval += rcvbuf.charAt(i++);
}
return retval;
}
public void run()
{
try {
try {
serverSocket = new ServerSocket(12666);
} catch (IOException ioe) {
JatLog.writePermLog("Could not listen on port: 12666. " + ioe);
System.exit(1);
}
try {
clientSocket = serverSocket.accept();
} catch (IOException ioe) {
JatLog.writePermLog("Accept failed. " + ioe);
System.exit(1);
}
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException ioe) {
JatLog.writePermLog("Init IP failed" + ioe);
}
String receive = "", type = "", value = "";
short id;
JatMessage message = null;
while (true) {
id = 0;
type = "";
value = "";
if (in.readLine().startsWith("<MSG>")) {
do {
receive = in.readLine();
if (receive.startsWith("<TYPE>")) {
type = parseString(receive, 6);
if (type.equalsIgnoreCase("message")) {
JatLog.writeTempLog("Message created");
message = new JatMessage(USER_RESPONSE_NONE);
message.setCompletion();
}
} else if (receive.startsWith("<HDR>")) {
value = parseString(receive, 5);
if (type.equalsIgnoreCase("message")) {
JatLog.writeTempLog("Setting header: " + value);
message.setSubject(value);
}
} else if (receive.startsWith("<BODY>")) {
value = parseString(receive, 6);
if (type.equalsIgnoreCase("message")) {
JatLog.writeTempLog("Setting body: " + value);
message.setBody(value);
}
} else if (receive.startsWith("<DEST>")) {
value = parseString(receive, 6);
if (type.equalsIgnoreCase("message")) {
JatLog.writeTempLog("Setting destination: " + value);
message.setCallNumber(value);
}
} else if (receive.startsWith("<ID>")) {
value = parseString(receive, 4);
if (type.equalsIgnoreCase("message")) {
id = Short.valueOf(value).shortValue();
}
} else if (receive.startsWith("</MSG>")) {
JatLog.writeTempLog("Message creation finished");
if (type.equalsIgnoreCase("message")) {
JatLog.writeTempLog("Sent message with id: " + message.send(id) + " from Java, refID: " + message.getRefID() );
}
} else {
JatLog.writeTempLog("ipClient received: " + receive);
}
} while ( !receive.startsWith("</MSG>") );
}
}
} catch (Exception e) {
JatLog.writePermLog("Error initiating IP communication: " + e);
}
try {
in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException ioe) {
JatLog.writePermLog("closeIP failed " + ioe);
}
return;
}
}
public static void main(String args[])
{
Client cli = new Client();
ipClient itst = cli.new ipClient();
JatSystem tester;
try{
tester = JatSystem.createJatSystem("3.00");
} catch (Exception e)
{
JatLog.writeTempLog(e.getMessage());
return;
}
try{
MyJatMessageHandler msgHandler = new MyJatMessageHandler();
tester.addService(msgHandler);
} catch (Exception e)
{
JatLog.writeTempLog("MessageHandler: " + e.getMessage());
}
try{
MyJatMessageConfHandler msgConfHandler = new MyJatMessageConfHandler();
tester.addService(msgConfHandler);
} catch (Exception e)
{
JatLog.writeTempLog("MessageConfHandler: " + e.getMessage());
}
try{
MyJatMessageConfRespHandler msgConfRespHandler = new MyJatMessageConfRespHandler();
tester.addService(msgConfRespHandler);
} catch (Exception e)
{
JatLog.writeTempLog("MessageConfRespHandler: " + e.getMessage());
}
try{
MyJatIMHandler IMHandler = new MyJatIMHandler();
tester.addService(IMHandler);
} catch (Exception e)
{
JatLog.writeTempLog("IMHandler: " + e.getMessage());
}
try{
MyJatIMRespHandler IMRespHandler = new MyJatIMRespHandler();
tester.addService(IMRespHandler);
} catch (Exception e)
{
JatLog.writeTempLog("IMRespHandler: " + e.getMessage());
}
try{
tester.execute();
} catch (Exception e)
{
JatLog.writeTempLog("Start system: " + e.getMessage());
return;
}
JatLog.writeTempLog("System ready.. waiting for event...");
itst.start();
}
}