Cannot read objects of two classes from stream - java

Good afternoon! I am writing a program that generates random parameters for a circle and a rectangle on the server. The data is formed correctly and is written correctly to the ObjectOutputStream on the server. After I want to read this data in the client, first I read the data for the circle (they come and output correctly, but after the circle the rectangle data is read. Everything seemed to be going well, but an exception occurred on the ObjectInputStream line java.io.StreamCorruptedException: invalid stream header: 7372000F.
Here is the server code
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8000);
Socket socket = ss.accept();
OutputStream os = socket.getOutputStream();
CircleParams []cp = new CircleParams[3];
for(int i=0;i<cp.length;i++)
{
cp[i] = new CircleParams();
System.out.println(cp[i]);
}
RectangleParams rp = new RectangleParams();
System.out.println(rp);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
objStream.writeObject(cp);
byte[] circle = byteStream.toByteArray();
byte[] lengthCircle = ByteBuffer.allocate(4).putInt(circle.length).array();
os.write(lengthCircle);
os.write(circle);
objStream.reset();
byteStream.reset();
objStream.writeObject(rp);
byte[] rects = byteStream.toByteArray();//Rectangle
byte[] lengthRect = ByteBuffer.allocate(4).putInt(rects.length).array();
os.write(lengthRect);
os.write(rects);
os.write(35343);
os.flush();
objStream.flush();
byteStream.close();
objStream.close();
os.close();
ss.close();
socket.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
And accordingly the client code
import java.io.*;
import java.net.Socket;
import java.nio.ByteBuffer;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("192.168.4.101", 8000);
InputStream is = s.getInputStream();
while (s != null && s.isConnected()) {
try {
byte[] lenghtMas = new byte[4];
is.read(lenghtMas);
int n = ByteBuffer.wrap(lenghtMas).asIntBuffer().get();
if (n > 0) {
byte[] message = new byte[n];
is.read(message);
ByteArrayInputStream bais = new ByteArrayInputStream(message);
ObjectInputStream ois = new ObjectInputStream(bais);
CircleParams[] cp = (CircleParams[]) ois.readObject();
for (int i = 0; i < cp.length; i++) {
System.out.println(cp[i]);
}
}
byte[] lenghtRect = new byte[4];
is.read(lenghtRect);
int m = ByteBuffer.wrap(lenghtRect).asIntBuffer().get();
if(m>0){
byte[] rect = new byte[m];
is.read(rect);
ByteArrayInputStream bais = new ByteArrayInputStream(rect);
ObjectInputStream ois = new ObjectInputStream(bais);
RectangleParams rp = (RectangleParams) ois.readObject();
System.out.println(rp);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
s.close();
is.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
And in the fields of the class, I registered two commands
for RectangleParams: private static final long serialVersionUID = 685098267757690L;
For CircleParams: private static final long serialVersionUID = 6529685098267757690L;
These fields are present in the server and client classes. The shape classes of the server and the client are the same.
How can I get rid of the error and receive the data correctly?
I checked all the code line by line in the debug. And I noticed that for RectangleParams the length of the byte array is transmitted correctly, but the array itself is incorrect. I also took advice from the Internet and assigned a variable of type int, reading an array from a stream. and I saw that there -1.
In the client, I expect to see data for building a circle, and data for building a rectangle. But now I see only the data for the circle, and below the exception text

Related

Java file transfer over Sockets trim last bytes

I have been trying to create a Messenger with file transfer capabilities, but I keep having too many null characters at the end. any time I use the file length to strip them, for some reason more of the file gets stripped and it just becomes a total mess. I'm not using any Java 7 or higher elements as I want it compatible with Java 6, and Windows 98 (grandma's PC).
I also get a lot of random null characters added into the file, im not sure how to avoid this
This is my code:
Transmit
package com.androdome.lunacoffee.management;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import com.androdome.lunacoffee.ErrorScreen;
import com.androdome.lunacoffee.SendScreen;
public class FileTransmitter implements Runnable{
String adds;
FileInputStream message;
int filecut = 4096;
byte[] fileName;
long fileSize;
SendScreen send;
public FileTransmitter(String address, FileInputStream msg, byte[] fnme, SendScreen snd, long l) {
adds = address;
send = snd;
message = msg;
fileName = fnme;
fileSize = l;
}
public void run()
{
try {
InetAddress add = InetAddress.getByName(adds);
Socket sock = new Socket(add, 11001);
DataOutputStream da = new DataOutputStream(sock.getOutputStream());
PrintWriter output = new PrintWriter(da);
da.write(fileName);
da.writeLong(message.getChannel().size());
byte[] filebuffer = new byte[filecut];
int g = 0;
int back = 0;
while((g = message.read(filebuffer)) != -1)
{
if(g != filecut && g > 0)
{
back = g;
}
da.write(filebuffer);
filebuffer = new byte[filecut];
}
da.writeInt(back);
System.out.print(back);
output.flush();
output.close();
send.incrementSent();
} catch (UnknownHostException e) {
send.incrementError();
// TODO Auto-generated catch block
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
new ErrorScreen("Unable to send file", "Your file was not able to send because the host \"" + adds + "\" was not availible!", sw.toString());
pw.close();
try {
sw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (IOException e) {
send.incrementError();
// TODO Auto-generated catch block
new ErrorScreen("Unable to send file", "Your file was not able to send due to a bad output stream!", e.getMessage());
}
}
}
Recieve:
package com.androdome.lunacoffee.management;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import com.androdome.lunacoffee.ErrorScreen;
import com.androdome.lunacoffee.FileScreen;
import com.androdome.lunacoffee.Main;
public class FileReciever implements Runnable {
int bufferSize = 4096;
int headerSize = 32;
byte[] filebuffer = new byte[bufferSize];
byte[] fileheader = new byte[headerSize];
Main main;
File downloadfile = new File("tmp");
File transferFile = new File("dnl.ldf");
public FileReciever(Main mn)
{
main = mn;
}
static byte[] trim(byte[] bytes)
{
int i = bytes.length - 1;
while (i >= 0 && bytes[i] == 0)
{
--i;
}
return Arrays.copyOf(bytes, i + 1);
}
public void run() {
try {
ServerSocket recieveSocket = new ServerSocket(11001);
while (this != null) {
try{
downloadfile.createNewFile();
Socket connectionSocket = recieveSocket.accept();
DataInputStream reader = new DataInputStream(connectionSocket.getInputStream());
reader.read(fileheader);
long fileSize = reader.readLong();
System.out.println(bufferSize);
filebuffer = new byte[bufferSize];
String fileName = new String(fileheader);
fileheader = new byte[headerSize];
FileOutputStream fw = new FileOutputStream(downloadfile);
while(reader.read(filebuffer) != -1)
{
fw.write(filebuffer);
filebuffer = new byte[bufferSize];
}
//reader.readInt();
reader.close();
fw.close();
//RandomAccessFile file = new RandomAccessFile(downloadfile, "Rwd");
//file.setLength(fileSize); // Strip off the last _byte_, not the last character
//file.close();
connectionSocket.close();
FileScreen fs = new FileScreen(downloadfile, fileName, connectionSocket.getInetAddress().getHostName());
fs.setVisible(true);
fs.setLocationRelativeTo(null);
}
catch(Exception ex)
{}
}
} catch (IOException e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
new ErrorScreen("Unable to start the File Recieve Thread", "Luna Messenger may already be running, or another program is using port 11001. Please close any program running on port 11001.", sw.toString());
pw.close();
try {
sw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
In your above code,i think it makes following mistake.
The first,you should add a number which indicate the filename's length.like this:
da.writeInt(fileName.length); //the added code
da.write(fileName);
On the FileReciever,the receive code is :
int fileNameLength = reader.readInt();
fileheader=new byte[fileNameLength];
read(reader,fileheader,0,fileNameLength);
the read method can read up to length bytes from input stream into a array of bytes until the stream is end.
public static int read(InputStream in, byte[] b, int off, int len) throws IOException {
if (len < 0) {
throw new IndexOutOfBoundsException("len is negative");
}
int total = 0;
while (total < len) {
int result = in.read(b, off + total, len - total);
if (result == -1) {
break;
}
total += result;
}
return total;
}
The second,it is not correctly that the FileTransmitter translate the file date to the FileReciever,and should not add a number at end.The appropriate approach is just writing file data to socket outputstream in FileTransmitter,and don't do any other things.Like this:
while((g = message.read(filebuffer)) != -1)
{
da.write(filebuffer,0,g);
}
On the orther hand,and how many bytes should be readed depend on the length of bytes that your readed from the socket's inputstream ago, when you read the bytes from socket oupustream into filebuffer.the receiver code:
int readLength;
int sumLength=0;
while((readLength=reader.read(filebuffer,0,(int)(fileSize-sumLength>filebuffer.length?filebuffer.length:fileSize-sumLength))) != -1){
sumLength+=readLength;
fw.write(filebuffer,0,readLength);
if(sumLength==fileSize){
break;
}
}

Java Server, Unity C# Client Are Freezing

I am building a Sphinx4 Java Server where Unity3D should communicate with. Sending audio data from Unity C# to the Java server works fine. Speech recognition with the received data works fine too. The problem appears when I try to send data from Java back to C#.
My current code:
JAVA
package main;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;
public class SpeechRecognition {
private static StreamSpeechRecognizer recognizer;
private static Configuration configuration;
public static void main(String[] args) {
configuration = new Configuration();
configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
try {
recognizer = new StreamSpeechRecognizer(configuration);
ServerSocket serverSocket = new ServerSocket(82);
while (System.in.available() == 0) {
Socket socket = serverSocket.accept();
System.out.println("Client found");
String recognized = RecognizeText(socket.getInputStream());
System.out.println("sending now");
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String json = recognized;
out.print(json);
out.flush();
out.close();
socket.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Quitting...");
serverSocket.close();
}
private static String RecognizeText(InputStream stream) throws Exception {
recognizer.startRecognition(stream);
SpeechResult result;
String resultString="";
while ((result = recognizer.getResult()) != null) {
resultString = result.getHypothesis();
System.out.format("Hypothesis: %s\n", resultString);
}
recognizer.stopRecognition();
return resultString;
}
}
Now my C# code is like this:
void Start () {
dataPath = Application.dataPath;
t = new Thread(Client);
t.Start();
}
private void Client()
{
String input;
TcpClient tcpClient = new TcpClient("localhost", 82);
NetworkStream networkStream = tcpClient.GetStream();
BinaryWriter bw = new BinaryWriter(networkStream);
var filepath = dataPath + "/Resources/audio/test.wav";
FileStream filestream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
BinaryReader filereader = new BinaryReader(filestream);
byte[] bytes = filereader.ReadBytes((Int32)filestream.Length);
bw.Write(bytes);
bw.Flush();
StreamReader streamReader = new StreamReader(networkStream);
input = streamReader.ReadToEnd();
print("Received data: " + input + "\n");
}
The recognition takes around 10 seconds. When te result is given the system freezes.
The Println("sending now") (in Java) is not printed. So it freezes before it even reaches it.
The weird thing is: When I only send text from Java to C# or from C# to Java, it works. If I want to send and receive on both ends, it freezes. And I need to send and receive data at the same time
I have found the anwser:
The loop is not broken in RecognizeText, placing the return statement within the while loop fixes the issue, as the recognized text will be returned first anyway.

it is strange that the server receive the data more than i had seed from the client

I have send a image that is about 500kb but in the server ,the image that receive is more than 500kb.I wonder that is there any logical problems I had made? the speed of the network is about 100kb/s.
the client
package client;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class client {
public static void main(String[] args) {
Socket s = null;
BufferedOutputStream bo = null;
BufferedInputStream bi = null;
try {
s = new Socket("127.0.0.1",12349);
bo = new BufferedOutputStream(s.getOutputStream());
bi = new BufferedInputStream(new FileInputStream("1.jpg"));
byte [] bys =new byte[1024];
while((bi.read(bys))!=-1){
bo.write(bys);
}
bo.flush();
System.out.println("already post the image");
bi.close();
} catch (IOException e) {
}finally{
try {
s.close();
} catch (IOException e) {
System.out.println("close failed");
}
}
}
}
server
package server;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class sserver {
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
BufferedOutputStream bo = null;
BufferedInputStream bs = null;
try {
ss = new ServerSocket(12349);
s = ss.accept();
bo = new BufferedOutputStream(new FileOutputStream("2.jpg"));
bs = new BufferedInputStream(s.getInputStream());
byte [] bys =new byte[1024];
while((bs.read(bys))!=-1){
bo.write(bys);
}
bo.flush();
System.out.println("upload success");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ss.close();
} catch (IOException e) {
System.out.println("close failed");
}
}
}
}
The problem is that you are disregarding the number of bytes read:
while((bi.read(bys))!=-1){
bo.write(bys);
}
This means that you always write 1024 bytes to bo, even if you read fewer than 1024 bytes from bi.
You need to assign the number of bytes read to a variable, and pass that to the write call:
int bytesRead;
while((bytesRead = bi.read(bys))!=-1){
bo.write(bys, 0, bytesRead);
}
Please write exacte byte which you received like below
int len= 0 ;
while( ( len = bi.read(bys))!=-1){
bo.write(bys,0, len);
}
Same code change required in both client and server side. I tested at my end and found that both file has same size afterthis change
Your server code is always writing full length of bys array into output stream.
Proper way to do it:
byte [] bys =new byte[1024];
while(true) {
int numRead = bs.read(bys);
if (numRead == -1)
break;
bo.write(bys, 0, numRead);
}

Send file over socket (with threading on server-side) - not working

I have a Client-Server programm. The Client-programm sends a file to the server and the server receives the file. my problem is, that the file is not really receiving on the server...I't creates a file.txt in the server-directory, but it is empty...(yes i'm sure that ne file.txt in the client-directory is not empty ;) )
I think the problem is the while-loop in Client.java, because it is never embarrassed....
For the future i implements now on the server side one thread per receiving file.
The client-programm:
package controller;
public class Main {
public static void main(String[] args) {
new Controller();
}
}
-
package controller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Controller {
public Controller() {
try {
sendFileToServer();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendFileToServer() throws UnknownHostException, IOException {
Socket socket = null;
String host = "localhost";
socket = new Socket(host, 5555);
String filename = "file.txt";
File file = new File(filename);
OutputStream outText = socket.getOutputStream();
PrintStream outTextP = new PrintStream(outText);
outTextP.println(filename);
long filesize = file.length();
byte[] bytes = new byte[(int) filesize];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
int count;
System.out.println("Start sending file...");
while ((count = bis.read(bytes)) > 0) {
System.out.println("count: " + count);
out.write(bytes, 0, count);
}
System.out.println("Finish!");
out.flush();
out.close();
fis.close();
bis.close();
socket.close();
}
}
-
The server-programm:
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
new Server();
}
}
-
public class Server {
private ServerSocket serverSocket;
public Server() {
try {
serverSocket = new ServerSocket(5555);
waitForClient();
} catch (IOException e) {
e.printStackTrace();
}
}
private void waitForClient() {
Socket socket = null;
try {
while(true) {
socket = serverSocket.accept();
Thread thread = new Thread(new Client(socket));
thread.start();
}
} catch (IOException ex) {
System.out.println("serverSocket.accept() failed!");
}
}
}
-
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client implements Runnable{
private Socket socket;
public Client(Socket socket) {
this.socket = socket;
}
#Override
public void run() {
receiveFile();
}
private void receiveFile() {
try {
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
int bufferSize = 0;
InputStream outText = socket.getInputStream();
// Get filename
InputStreamReader outTextI = new InputStreamReader(outText);
BufferedReader inTextB = new BufferedReader(outTextI);
String dateiname = inTextB.readLine();
System.out.println("Dateiname: " + dateiname);
try {
is = socket.getInputStream();
bufferSize = socket.getReceiveBufferSize();
System.out.println("Buffer size: " + bufferSize);
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}
try {
fos = new FileOutputStream(dateiname);
bos = new BufferedOutputStream(fos);
} catch (FileNotFoundException ex) {
System.out.println("File not found.");
}
byte[] bytes = new byte[bufferSize];
int count;
while ((count = is.read(bytes)) > 0) {
bos.write(bytes, 0, count);
System.out.println("This is never shown!!!"); // In this while-loop the file is normally receiving and written to the directory. But this loop is never embarrassed...
}
bos.flush();
bos.close();
is.close();
socket.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
When you do these kind of transfers you have to keep in mind that there is a difference between a socket's close and shutdown, in your code you close the socket in the client.
So lets see what happens : you fill in the buffers then you tell the socket to close which will discard the operation you just asked for.
When you shutdown you tell the socket "I won't send more data but send what's left to be sent and shut down" so what you need to do is to shut down the socket before you close it so the data will arrive.
So instead of this
out.flush();
out.close();
fis.close();
bis.close();
socket.close();
Try it with this
out.flush();
socket.shutdownInput(); // since you only send you may not need to call this
socket.shutdownOutput(); // with this you ensure that the data you "buffered" is sent
socket.close();
Generally if you want a graceful close, you should do it like this in all cases even for the server, so what you did is usually okay if there is an error and you just close the connection since you cant recover from an error.

keeping a java socket open?

i'm making a program/game that will update automatically. i have the update part down, but not the checking of the version. i would have thought that it'd be pretty easy. heres what i've done. i wrote an updater for the game, and i wrote a server. the server starts a thread every time a client/updater connects. the thread handles everything. the game updater reads a file called version.txt and that provides the version number (default 0.0.1) and sends it to the server. the server does recieve the version, and will System.out.println(); if the version matches, and if i change the version, it changes the output. so that part works. but that is as far as it goes. the second part of the process is that the server then sends just a text file called NPS Game.txt (it sends anything, but txt was easy to test with) and the client replaces the old version of this file with the new one that just sent. the problem is that i keep getting an error that says the Socket is closed. i've tried using socket.setKeepAlive(true); but that didnt change anything (i put that on both the client and the server). here is the code:
server:
package main;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class Server {
static ServerSocket serverSocket = null;
static Socket clientSocket = null;
static boolean listening = true;
public static void main(String[] args) throws IOException {
try {
serverSocket = new ServerSocket(6987);
} catch (IOException e) {
ServerThread.showmsg("Could not use port: 6987");
System.exit(-1);
}
ServerThread.showmsg("server- initialized");
ServerThread.showmsg("server- waiting...");
while (listening)
new ServerThread(serverSocket.accept()).start();
}
}
server thread:
package main;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import javax.swing.JOptionPane;
public class ServerThread extends Thread {
Socket socket;
ObjectInputStream in;
ObjectOutputStream out;
String version = "0.0.1";
public ServerThread(Socket socket) {
super("Server Thread");
this.socket = socket;
}
public void run() {
showmsg("server- Accepted connection : " + socket);
getVersion();
sendFile();
}
public void getVersion() {
try {
ObjectInputStream ois = new ObjectInputStream(
socket.getInputStream());
try {
String s = (String) ois.readObject();
if (s.equals(version)) {
System.out.println("server- matched version :)");
} else {
System.out.println("server- didnt match version :(");
System.exit(0);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendFile() {
// sendfile
File myFile = new File("C:\\Programming\\NPS\\Files\\bin\\NPS Game.txt");
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis;
try {
fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = socket.getOutputStream();
showmsg("server- Sending...");
os.write(mybytearray, 0, mybytearray.length);
os.flush();
socket.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void showmsg(String s) {
JOptionPane.showMessageDialog(null, s);
}
}
and the client/updater:
package main;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
import org.omg.CORBA.portable.InputStream;
public class Connections {
String IP, port;
String message = "";
Socket socket;
public Connections(boolean server, boolean updating, String IP, String port) {
this.IP = IP;
this.port = port;
try {
socket = new Socket(IP, Integer.parseInt(port));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (!server) {
if (updating) {
try {
sendVersion();
updating();
} catch (IOException e) {
e.printStackTrace();
}
} else {
client();
}
}
if (server) {
}
}
public void sendVersion() throws IOException {
FileReader fileReader = new FileReader(
"C:\\Program Files\\AVTECH\\NPS\\Files\\bin\\version.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String stringRead = bufferedReader.readLine();
bufferedReader.close();
ObjectOutputStream oos = new ObjectOutputStream(
socket.getOutputStream());
oos.writeObject(stringRead);
oos.flush();
oos.close();
}
public void updating() throws IOException {
int filesize = 6022386; // filesize temporary hardcoded
int bytesRead;
int current = 0;
showmsg("client- connected");
// receive file
byte[] byteArray = new byte[filesize];
java.io.InputStream inStream = socket.getInputStream();
FileOutputStream fileOutStream = new FileOutputStream(
"C:\\Program Files\\AVTECH\\NPS\\Files\\bin\\NPS Game.txt");
BufferedOutputStream buffOutStream = new BufferedOutputStream(
fileOutStream);
bytesRead = inStream.read(byteArray, 0, byteArray.length);
current = bytesRead;
do {
bytesRead = inStream.read(byteArray, current,
(byteArray.length - current));
if (bytesRead >= 0)
current += bytesRead;
} while (bytesRead > -1);
buffOutStream.write(byteArray, 0, current);
buffOutStream.flush();
buffOutStream.close();
inStream.close();
socket.close();
}
public static void showmsg(String s) {
JOptionPane.showMessageDialog(null, s);
}
}
i dont know what's wrong with it, but it is really frusturating. if anyone can help, it would be appreciated. some things ive done: google all kinds of questions, tried implementing socket.setKeepAlive(true);. also, i thought it might be of note, in the server thread, right above the line BufferedInputStream bis = new BufferedInputStream(fis);, i put System.out.println(socket.isClosed); and it returned true. thats all i have. thanks in advance!
I think that closing one of both streams, closes the socket. So try to remove the ois.close() call out of your getVersion() method at the server side. Also get rid of the oos.close() call in your sendVersion() method at the client side.
When you construct an ObjectOutputStream or ObjectInputStream and you are done with it, you shouldn't close that stream, because it will close the underlying stream, which is in your case the socket.

Categories