Socket get incomplete data from server - java

I have java application deployed in weblogic the principal role from app is the communication with another API via socket. This another API is installed in another server. Ok
Sometime to here the java app started getting incomplete data from socket. I have analyzed the logs on the API side. The API return complete data but on weblogic server where java app is deployed not read complete data. Locally the java app work as well.
I suppose that it have something with server memory. I have no idea to solve this problem.
I search on google but have no answers.
Below this original message returned locally with succes:
#**#0 100-Autorizado o uso da NF-e 33170845242914003465652000000007511200000067 http://www4.fazenda.rj.gov.br/retrieve/QRCode?chNFe=33170845242914003465652000000007511200000067&nVersao=100&tpAmb=2&cDest=12345678998&dhEmi=323031372d30382d31385431333a30343a32302d30333a3030&vNF=110.00&vICMS=22.80&digVal=6b6e3774696b652f616b4878574a35414d766367347074497a62343d&cIdToken=000001&cHashQRCode=998144c803f3f5e56a0e1764647ccd6928c2a70d 333170000817607|2017-08-18T13:04:23-03:00|SVRSnfce201707171030|100 http://www4.fazenda.rj.gov.br/consultaNFCe/QRCode? 6989*##*
Below is message returned in weblogic server incomplete:
#**#0 100-Autorizado o uso da NF-e 33170845242914003465652000000007501000000449 http://www4.fazenda.rj.gov.br/consultaNFCe/QRCode?chNFe=33170845242914003465652000000007501000000449&nVersao=100&tpAmb=2&cDest=22233344405&dhEmi=323031372d30382d31385431323a35343a30342d30333a3030&vNF=325.00&vICMS=61.75&digVal=7955796b6b4e64687a7342335470755166495847516c4343566e453d&cIdToken=000001&cHashQRCode=03e10ca5299ed5cb5acd7de5dc5db98df9c49d0e 333170000817573|2017-08-18T12:54:06-03:00|SVRSnfce201707171030|100 http://www4.fazenda.rj.go
Below is responsible class to send and read data:
#Repository
public class SocketRepository implements BaseRepository {
private static final Logger LOGGER = LoggerFactory.getLogger(SocketRepository.class);
private Socket socket;
private static int BUFFER_SIZE = 2048;
private BufferedOutputStream bos;
private BufferedInputStream bis;
#Override
public synchronized String sendDataSocket(String buff, final OsbRequestDTO osbDto) throws Exception {
try {
LOGGER.info("start send data. ");
URL url = new URL(osbDto.getEmissor().getUrlLoja());
openConnection(url.getHost(), url.getPort());
if (this.bos == null) {
this.bos = new BufferedOutputStream(this.socket.getOutputStream(), BUFFER_SIZE);
}
if (this.bis == null) {
this.bis = new BufferedInputStream(this.socket.getInputStream(), BUFFER_SIZE);
}
this.bos.write(buff.getBytes(StandardCharsets.UTF_8));
this.bos.flush();
LOGGER.info("end send data. ");
return readMessagePaperless(bis);
} catch (Exception e) {
LOGGER.error("Erro :" + e.getMessage());
throw e;
} finally {
LOGGER.info("start finally block. ");
closeConnection();
LOGGER.info("End finally block.");
}
}
private String readMessagePaperless(final BufferedInputStream bis) throws IOException {
try {
LOGGER.info("start read data .");
byte[] bufferSize = new byte[512 * 1024];
int data = bis.read(bufferSize);
if (data == -1) {
return "";
}
LOGGER.info("end read data.");
return new String(bufferSize, 0, data, StandardCharsets.UTF_8);
} catch (Exception e) {
LOGGER.error("err." + e.getMessage());
throw e;
}
}
private void closeConnection() throws Exception {
try {
if (this.bos != null && this.bis != null) {
try {
this.bos.close();
this.bis.close();
} catch (Exception e) {
LOGGER.error("err :" + e.getMessage());
throw e;
}
}
if (this.socket == null) {
return;
}
this.socket.close();
this.socket = null;
this.bis = null;
this.bos = null;
} catch (Exception e) {
LOGGER.error("err :" + e.getMessage());
throw e;
}
}
private void openConnection(final String host, final int port) throws UnknownHostException, IOException {
LOGGER.info("start open socket conn.");
if (this.socket != null) {
return;
}
this.socket = new Socket(host, port);
LOGGER.info("end.");
}
}

The read will only read a chunk of data as provided by the network, normally around 1500 bytes depending on various factors. You should continue reading until you have received the complete message. When the connection is local it works differently.

Related

Java TCP BufferedOutputStream reconnection strategy

I have an application that has to send data via TCP socket to another application. This is a 1 way stream from client to server. When sending data the client must retry/reconnect and try to insure all data is sent should the receiver/listener/server die/disappear or drop the connection. My code is as follow:
public class TCPSocket implements Closeable {
private static final int SIXTY_FOUR_KB = 65536;
private final String ip;
private final int port;
private Socket socket;
private BufferedOutputStream writer;
public TCPSocket(String ip, int port) {
this.ip = ip;
this.port = port;
}
public TCPSocket connect() throws ConnectException {
try {
socket = new Socket(ip, port);
socket.setSendBufferSize(SIXTY_FOUR_KB);
writer = new BufferedOutputStream(socket.getOutputStream(), SIXTY_FOUR_KB);
} catch (Exception e) {
throw new ConnectException(e.getMessage());
}
return this;
}
public void write(String message) throws InterruptedException {
boolean succeeded = true;
do {
try {
writer.write(message.getBytes(StandardCharsets.UTF_8));
writer.write("\n".getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
System.out.println(e.getMessage());
succeeded = false;
// Exponential backoff to go here
try {
System.out.println("Attempting reconnection");
tryClose();
connect();
} catch (ConnectException connectException) {
System.out.println(connectException.getMessage());
}
}
} while (!succeeded);
}
private void tryClose() {
try {
close();
} catch (Exception ex) {
System.out.println("Failed closing TCPSocket");
}
}
#Override
public void close() throws IOException {
if (writer != null) {
writer.flush();
writer.close();
writer = null;
}
if (socket != null && !socket.isClosed()) {
socket.close();
socket = null;
}
}
}
N.B: Reason for using the BufferedOutputStream is because I'm sending small messages and all other methods couldn't get the same throughput in real world test scenario.
This all works as expected for me however I have a few points.
Is this the right way to do this or totally insane and will cause
serious problems?
When trying to clean up and close connections and the writer before
opening a new connection the following error is thrown and I am
unable to close the bufferedOutputStream
java.net.SocketException: Connection reset by peer: socket write error
If I socket.shutdownOutput(); before attempting to close the output stream then that also throws an exception. What is the correct way to clean up and reconnect?

Android: Bluetooth - How to read incoming data

I have successfully paired and connected with a Bluetooth device. I am now interested in receiving all data being transferred between the 2 and seeing whats what.
I am getting the input stream from the socket and attempting to read it. I return this and just log it.
The only way I know of doing this from what I have read is just do read with a byte buffer to return an int. However I should have loads of data coming through. How can I continually read out data being transferred, and also format as bytes rather than an int.
Thanks.
Full code below:
public class ConnectThread {
private BluetoothSocketWrapper bluetoothSocket;
private BluetoothDevice device;
private boolean secure;
private BluetoothAdapter adapter;
private List<UUID> uuidCandidates;
private int candidate;
/**
* #param device the device
* #param secure if connection should be done via a secure socket
* #param adapter the Android BT adapter
* #param uuidCandidates a list of UUIDs. if null or empty, the Serial PP id is used
*/
public ConnectThread(BluetoothDevice device, boolean secure, BluetoothAdapter adapter,
List<UUID> uuidCandidates) {
this.device = device;
this.secure = secure;
this.adapter = adapter;
this.uuidCandidates = uuidCandidates;
if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) {
this.uuidCandidates = new ArrayList<UUID>();
this.uuidCandidates.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
}
}
public BluetoothSocketWrapper connect() throws IOException {
boolean success = false;
while (selectSocket()) {
adapter.cancelDiscovery();
try {
bluetoothSocket.connect();
success = true;
break;
} catch (IOException e) {
//try the fallback
try {
bluetoothSocket = new FallbackBluetoothSocket(bluetoothSocket.getUnderlyingSocket());
Thread.sleep(500);
bluetoothSocket.connect();
success = true;
break;
} catch (FallbackException e1) {
Log.w("BT", "Could not initialize FallbackBluetoothSocket classes.", e);
} catch (InterruptedException e1) {
Log.w("BT", e1.getMessage(), e1);
} catch (IOException e1) {
Log.w("BT", "Fallback failed. Cancelling.", e1);
}
}
}
if (!success) {
throw new IOException("Could not connect to device: "+ device.getAddress());
}
receiveData(bluetoothSocket);
return bluetoothSocket;
}
private boolean selectSocket() throws IOException {
if (candidate >= uuidCandidates.size()) {
return false;
}
BluetoothSocket tmp;
UUID uuid = uuidCandidates.get(candidate++);
Log.i("BT", "Attempting to connect to Protocol: "+ uuid);
if (secure) {
tmp = device.createRfcommSocketToServiceRecord(uuid);
} else {
tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
}
bluetoothSocket = new NativeBluetoothSocket(tmp);
return true;
}
public static interface BluetoothSocketWrapper {
InputStream getInputStream() throws IOException;
OutputStream getOutputStream() throws IOException;
String getRemoteDeviceName();
void connect() throws IOException;
String getRemoteDeviceAddress();
void close() throws IOException;
BluetoothSocket getUnderlyingSocket();
}
public static class NativeBluetoothSocket implements BluetoothSocketWrapper {
private BluetoothSocket socket;
public NativeBluetoothSocket(BluetoothSocket tmp) {
this.socket = tmp;
}
#Override
public InputStream getInputStream() throws IOException {
return socket.getInputStream();
}
#Override
public OutputStream getOutputStream() throws IOException {
return socket.getOutputStream();
}
#Override
public String getRemoteDeviceName() {
return socket.getRemoteDevice().getName();
}
#Override
public void connect() throws IOException {
socket.connect();
}
#Override
public String getRemoteDeviceAddress() {
return socket.getRemoteDevice().getAddress();
}
#Override
public void close() throws IOException {
socket.close();
}
#Override
public BluetoothSocket getUnderlyingSocket() {
return socket;
}
}
public class FallbackBluetoothSocket extends NativeBluetoothSocket {
private BluetoothSocket fallbackSocket;
public FallbackBluetoothSocket(BluetoothSocket tmp) throws FallbackException {
super(tmp);
try
{
Class<?> clazz = tmp.getRemoteDevice().getClass();
Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
Method m = clazz.getMethod("createRfcommSocket", paramTypes);
Object[] params = new Object[] {Integer.valueOf(1)};
fallbackSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params);
}
catch (Exception e)
{
throw new FallbackException(e);
}
}
#Override
public InputStream getInputStream() throws IOException {
return fallbackSocket.getInputStream();
}
#Override
public OutputStream getOutputStream() throws IOException {
return fallbackSocket.getOutputStream();
}
#Override
public void connect() throws IOException {
fallbackSocket.connect();
}
#Override
public void close() throws IOException {
fallbackSocket.close();
}
}
public static class FallbackException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public FallbackException(Exception e) {
super(e);
}
}
public void sendData(BluetoothSocketWrapper socket, int data) throws IOException{
ByteArrayOutputStream output = new ByteArrayOutputStream(4);
output.write(data);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(output.toByteArray());
}
public int receiveData(BluetoothSocketWrapper socket) throws IOException{
byte[] buffer = new byte[256];
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
InputStream inputStream = socket.getInputStream();
inputStream.read(buffer);
return input.read();
}
}
In the first place, stop using ByteArrayInputStream and ByteArrayOutputStream for more control.
If the socket sends/receives text, do this.
TO SEND:
String text = "My message";
socketOutputStream.write(text.getBytes());
TO RECEIVE:
int length = socketInputStream.read(buffer);
String text = new String(buffer, 0, length);
The socketOutputStream should be your bluetoothSocket.getOutputStream().
If the socket sends/receives large loads of data, the key is the while loop to prevent out of memory exceptions. The data will be read by chunks of (for example every 4KB of buffer size), when you choose the buffer size, consider the heap size, if you're live-streaming media, consider latency and quality too.
TO SEND:
int length;
while ((length = largeDataInputStream.read(buffer)) != -1) {
socketOutputStream.write(buffer, 0, length);
}
TO RECEIVE:
int length;
//socketInputStream never returns -1 unless connection is broken
while ((length = socketInputStream.read(buffer)) != -1) {
largeDataOutputStream.write(buffer, 0, length);
if (progress >= dataSize) {
break; //Break loop if progress reaches the limit
}
}
FAQ:
How do I get the size of receiving data? You'll have to make your own implementation to notify remote device to get ready to receive data (including file size), this will require at least a dual-socket connection (2 sockets, 1 device), for example, 1 socket for text fragments and custom commands, and 1 socket for large data, like files or streaming.
What are largeDataInputStream and largeDataOutputStream? These streams can be normal I/O streams, FileInputStream/FileOutputStream or etc.
Why the while loop for BluetoothSocket never finishes? The socket input is continuously receiving data, and the read() methods blocks itself until data is detected. To prevent blocking the code in that line, while loop must be broken.
NOTE: This answer could need an edit. I'm not a native English speaker.
Following the above advice, I am now using this code to retrieve data.
public void receiveData(BluetoothSocketWrapper socket) throws IOException{
InputStream socketInputStream = socket.getInputStream();
byte[] buffer = new byte[256];
int bytes;
// Keep looping to listen for received messages
while (true) {
try {
bytes = socketInputStream.read(buffer); //read bytes from input buffer
String readMessage = new String(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity via handler
Log.i("logging", readMessage + "");
} catch (IOException e) {
break;
}
}
}

Java chat application only picking up some sent messages

I'm writing an all-in-one java chat program which will either act as a client or a server. I'm currently having this problem where, after the connection is established, the program only successfully recieves (or sends?) some of the messages. I've used a loop to spam through a load of junk and I've seen that the other end will only pick up some of the messages. I've never got a message to send manually.
Here is the code:
public class ConnectionThread implements Runnable {
private Connection c = Connection.getInstance();
private ChatInterface chatInterface;
private static ConnectionThread serverThread;
private ServerSocket serverSocket;
private Socket socket;
private ObjectInputStream dataIn;
private ObjectOutputStream dataOut;
public ConnectionThread() {}
public static synchronized ConnectionThread getInstance() {
if (serverThread == null) {
serverThread = new ConnectionThread();
}
return serverThread;
}
public void run() {
// If the programs role is server, set up the server
if (c.getRole() == ConnectionRole.SERVER) {
try {
setupServer();
waitForConnection();
setupStreams();
} catch (IOException e) {
e.printStackTrace();
}
do {
try {
chatInterface.addToChatHistory(dataIn.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
} while (c.getRole() == ConnectionRole.SERVER);
}
// If the programs role is client, set up a connection to the server
if (c.getRole() == ConnectionRole.CLIENT) {
try {
setupClient();
setupStreams();
} catch (IOException e) {
e.printStackTrace();
}
do {
try {
chatInterface.addToChatHistory(dataIn.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
} while (c.getRole() == ConnectionRole.CLIENT);
}
}
private void setupClient() throws IOException {
System.out.println("ATTEMPTING TO CONNECT...");
socket = new Socket("127.0.0.1", 8080);
System.out.println("CONNECTED!");
}
private void setupServer() throws IOException {
System.out.println("SETTING UP SERVER..");
serverSocket = new ServerSocket(8080, 1);
System.out.println("SERVER SETUP");
}
private void waitForConnection() throws IOException {
System.out.println("WAITING FOR A CONNECTION...");
socket = serverSocket.accept();
System.out.println("CONNECTION RECIEVED");
}
private void setupStreams() throws IOException {
System.out.println("SETTING UP STREAMS...");
dataOut = new ObjectOutputStream(socket.getOutputStream());
dataIn = new ObjectInputStream(socket.getInputStream());
chatInterface = ChatInterface.getInstance();
System.out.println("STREAMS SETUP");
}
public void sendMessage(String message) throws IOException {
System.out.println("SENDING MESSAGE...");
dataOut.writeUTF(message);
chatInterface.addToChatHistory(message);
System.out.println("MESSAGE SENT!");
}
}
Can anyone tell me why not all messages are being sent/picked up properly? I've been playing with it for quite a while now and I can't figure out why.
I found out after following EJP's recommendation to switch to DataInput/OutputStreams which worked straight away. Although I did need to be using ObjectInput/OutputStreams so I switched back. I found that I got the same issue again, until I switched to write/readObject instead of write/readUTF. If I cast the readObject to (String) it would then manage to receive every message.
So if anyone is having the same problem with ObjectInput/OutputStreams using write/readUTF try using write/readObject instead.

Java Sockets sending multiple objects to the same server

I'm trying to send multiple Objects through a socket to a java server.
To have a gerneral type I convert my messages into an instance of the class Message and send this object to the server.
I wrote a little testclass, which sends three objects to the server.
The problem is, only one objects reaches the server.
I tried nearly everything, without success.
My Server:
public class Server {
private ServerConfig conf = new ServerConfig();
private int port = Integer.parseInt(conf.loadProp("ServerPort"));
Logger log = new Logger();
ServerSocket socket;
Chat chat = new Chat();
public static void main(String[] args) {
Server s = new Server();
if (s.runServer()) {
s.listenToClients();
}
}
public boolean runServer() {
try {
socket = new ServerSocket(port);
logToConsole("Server wurde gestartet!");
return true;
} catch (IOException e) {
logToConsole("Server konnte nicht gestartet werden!");
e.printStackTrace();
return false;
}
}
public void listenToClients() {
while (true) {
try {
Socket client = socket.accept();
ObjectOutputStream writer = new ObjectOutputStream(client.getOutputStream());
Thread clientThread = new Thread(new Handler(client, writer));
clientThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void logToConsole(String message) {
System.out.print(message);
}
public class Handler implements Runnable {
Socket client;
ObjectInputStream reader;
ObjectOutputStream writer;
User user;
public Handler(Socket client, ObjectOutputStream writer) {
try {
this.client = client;
this.writer = writer;
this.reader = new ObjectInputStream(client.getInputStream());
this.user = new User();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
while (true) {
Message incomming;
try {
while ((incomming = (Message) reader.readUnshared()) != null) {
logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n");
logToConsole(
"Vom Client: \n" + incomming.getType() + "-----" + incomming.getValue().toString());
handle(incomming);
}
} catch (SocketException se) {
se.printStackTrace();
Thread.currentThread().interrupt();
} catch (IOException ioe) {
ioe.printStackTrace();
Thread.currentThread().interrupt();
} catch (ClassNotFoundException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
private void handle(Message m) throws IOException {
String type = m.getType();
if (type.equals(config.ConstantList.Network.CHAT.toString())) {
chat.sendMessage(m);
} else if (type.equals(config.ConstantList.Network.LOGIN.toString())) {
System.out.println(user.login(m.getValue().get(0), writer));
System.out.println(m.getValue().get(0));
}
}
}
}
The Client:
public class Connect {
Socket client = null;
ObjectOutputStream writer = null;
ObjectInputStream reader = null;
private Config conf = new Config();
//private String host = conf.loadProp("ServerIP");
String host = "localhost";
private int port = Integer.parseInt(conf.loadProp("ServerPort"));
public boolean connectToServer() {
try {
client = new Socket(host, port);
reader = new ObjectInputStream(client.getInputStream());
writer = new ObjectOutputStream(client.getOutputStream());
logMessages("Netzwerkverbindung hergestellt");
Thread t = new Thread(new MessagesFromServerListener());
t.start();
return true;
} catch (Exception e) {
logMessages("Netzwerkverbindung konnte nicht hergestellt werden");
e.printStackTrace();
return false;
}
}
public boolean isConnectionActive() {
if (client == null || writer == null || reader == null){
return false;
}else{
return true;
}
}
public void sendToServer(Message m) {
try {
writer.reset();
writer.writeUnshared(m);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
And I try to send the objects with the class:
public void sendChatMessage(String username, String message) throws InterruptedException {
ChatMessage cm = new ChatMessage();
cm.setChat(username, null, message);
Message m = new Message(cm);
conn.sendToServer(m);
System.out.println("SENDED");
}
public static void main(String[] args) throws InterruptedException {
String username = "testuser";
String chatmessage = "Hallo Welt!";
connect.connect();
sendChatMessage(username, chatmessage);
sendChatMessage(username, chatmessage);
sendChatMessage(username, chatmessage);
}
I know that this is always the same message, but it is only for test purposes.
The messages are the objects they are Serializable and with only one object it works as designed.
Does anyone can see where I made my mistake?
while ((incomming = (Message) reader.readUnshared()) != null) {
Here you are reading an object, and blocking until it arrives.
logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n");
Here you are reading another object, and blocking till it arrives, and then erroneously logging it as the object you already read in the previous line.
Instead of logging reader.readObject(), you should be logging the value of incoming, which you have also misspelt.
And the loop is incorrect. readObject() doesn't return null at end of stream: it throws EOFException. It can return null any time you write null, so using it as a loop termination condition is completely wrong. You should catch EOFException and break.
Found the solution, the line logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n"); in the Server class, blocks the connection.

Java Client/Server Socket Broken Pipe

I'm creating an update client via Sockets and I'm getting a Broken Pipe on the server side. The server accepts a client socket and responds to the same socket with either a message or a large byte array (~180MB). The error does not happen when testing locally (both client and server on the same machine) and it seems that it happens while sending the byte array. I'm not specifying a time out on the client socket and don't know why it is closing before reading the full response. Its my first time working with sockets and any help would be appreciated.
My Client Socket Code:
public static Response makeRequest(Request req) throws IOException {
Response response = null;
Socket echoSocket = null;
ObjectOutputStream out = null;
ObjectInputStream in = null;
echoSocket = new Socket(serverHost, 10008);
out = new ObjectOutputStream(echoSocket.getOutputStream());
in = new ObjectInputStream(
echoSocket.getInputStream());
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
out.writeObject(req);
try {
response = (Response)in.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
return response;
}
Response is just a POJO holding the response (string/byte[] and other data)
My Server Code (copied an example of Sun/Oracle site and added my code to it)
public class Server extends Thread {
private Socket clientSocket;
public Server(Socket clientSocket) {
this.clientSocket = clientSocket;
start();
}
public void run()
{
{
System.out.println ("New Communication Thread Started");
try {
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
Request request = null;
try {
request = (Request)in.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UpdateDAO dao = new UpdateDAO();
ClientDAO cdao = new ClientDAO();
Update update = null;
Client client = null;
Session s = HibernateUtil.currentSession();
Transaction t = s.beginTransaction();
if (request != null) {
client = cdao.getClient(request.getClientId());
LogItem log = new LogItem();
log.setClient(client);
log.setTimestamp(new Date());
log.setAction(request.getAction());
if (request.getResponse() != null) {
update = dao.getUpdate(request.getResponse().getUpdateId());
}
TaskContext ctx = new TaskContext(request, client, update, log);
System.out.println("Action: " + request.getAction().getDescription());
Task task = TaskFactory.getTask(request.getAction());
System.out.println(task.getClass().getName());
Response response = task.perform(ctx);
out.writeObject(response);
log.setClientTaskDescription(request.getMessage());
log.setUpdate(ctx.getUpdate());
dao.save(ctx.getLog());
if (ctx.getUpdate() != null) {
dao.update(ctx.getUpdate());
}
} else {
out.writeObject(new Response("what"));
}
t.commit();
out.close();
in.close();
clientSocket.close();
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
}
}
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(10008);
System.out.println ("Connection Socket Created");
try {
while (true)
{
System.out.println ("Waiting for Connection");
new Server (serverSocket.accept());
}
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
}
catch (IOException e)
{
System.err.println("Could not listen on port: 10008.");
System.exit(1);
}
finally
{
try {
serverSocket.close();
}
catch (IOException e)
{
System.err.println("Could not close port: 10008.");
System.exit(1);
}
}
}
}
If the client is, in fact, running out of memory:
java -Xmx512m -jar <the jar>
or
java -Xmx512m com.foo.blah.YourClass
would increase the maximum heap for the client/server. Keep in mind you may have to increase the heap for both sides of the pipe since both sides would be reading all ~180mb into memory at runtime.

Categories

Resources