S
sani
Gast
Hi
I want to move files (ascii and binarys) between two systems and have to use sockets for that. Now, I wrote this simple program (for test only):
And the client-class:
And now my problem:
The client is not able to recognize the end of the stream (-1). If I close the Server-OutputStream, the client is go on. Otherwise the client waits for new data to read. What I'm doing wrong? And, how to say the client, that the there are no more data, without closing the Server-OutputStream?
Tahnks for any help and HAPPY NEW YEAHR.
I want to move files (ascii and binarys) between two systems and have to use sockets for that. Now, I wrote this simple program (for test only):
Code:
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
public static void main(String[] args) throws IOException
{
String fileName = "note.txt";
ServerSocket server = new ServerSocket(8080);
FileInputStream fis = new FileInputStream(fileName);
System.out.println("\nServer is ready: 8080\n");
Socket client = server.accept();
InputStream socketInput = client.getInputStream();
OutputStream socketOutput = client.getOutputStream();
// wait for ok (if n == 0)
int n = socketInput.read();
if (n == 0)
{
byte[] buffer = new byte[10];
int count = 0;
count = fis.read(buffer);
while (count != -1)
{
// send data
socketOutput.write(buffer);
// read data from file again
count = fis.read(buffer);
}
}
// only to avoid closing of the 'socketOutput'
Scanner sc = new Scanner(System.in);
System.out.print("Enter an int-number: ");
sc.nextInt();
socketOutput.close();
}
}
And the client-class:
Code:
import java.io.*;
import java.net.*;
import java.util.*;
public class Client
{
public static void main(String[] args) throws UnknownHostException,
IOException
{
FileOutputStream fos = new FileOutputStream("C:/Temp/client/fileCopied.txt");
Socket client = new Socket("127.0.0.1", 8080);
OutputStream socketOutput = client.getOutputStream();
InputStream socketInput = client.getInputStream();
int n = 0;
// send OK
socketOutput.write(n);
byte[] buffer = new byte[10];
int count = 0;
// read data
count = socketInput.read(buffer);
while (count != -1)
{
// write data into file
fos.write(buffer);
// read data from socket-InputStream again
count = socketInput.read(buffer); // here waits the client for new data!!!
}
fos.close();
// only for debug
Scanner sc = new Scanner(System.in);
System.out.print("enter an int-number: ");
int x = sc.nextInt();
}
}
And now my problem:
The client is not able to recognize the end of the stream (-1). If I close the Server-OutputStream, the client is go on. Otherwise the client waits for new data to read. What I'm doing wrong? And, how to say the client, that the there are no more data, without closing the Server-OutputStream?
Tahnks for any help and HAPPY NEW YEAHR.