Binary data til Server
Hej Jeg har et lille problem:Jeg skal lave en kommunikation til serve med en klient der skal sende binary data. Serveren skal så sende retur med en text"String".
Er det muligt at bruge DataOutputStream til at sende til Serveren? Er det ikke binært?
Jeg har forsøgt.. Men får en fejl:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.readInt(Unknown Source)
at DateServer.main(DateServer.java:42)
Her er min Klient:
public class DateClient {
/**
* @param args
*/
@SuppressWarnings("null")
public static void main(String[] args) {
boolean done = false;
Scanner in = null;
DataOutputStream dataOut = null;
InputStream inStream = null;
OutputStream outStream = null;
Socket s = null;
try {
s = new Socket("127.0.0.1", 8199);
s.setSoTimeout(10000);
inStream = s.getInputStream();
outStream = s.getOutputStream();
in = new Scanner(inStream);
Scanner text = new Scanner(System.in);
new DataOutputStream(outStream);
String serverOutput = in.nextLine();
System.out.println("Connection received from "
+ s.getInetAddress().getHostName());
System.out.println("echo: " + serverOutput);
while (!done) {
String input = text.nextLine();
int convertString = Integer.parseInt(input);
System.out.println(convertString);
dataOut.writeInt(convertString);
// out.println(input);
dataOut.flush();
serverOutput = in.nextLine();
System.out.println("echo: " + serverOutput);
// if (input.equals("BYE")) {
// done = true;
// }
}
s.close();
dataOut.close();
inStream.close();
outStream.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Her er min Server:
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class DateServer {
/**
* @param args
*/
public static void main(String[] args) {
try
{
// establish server socket
ServerSocket s = new ServerSocket(8199);
// wait for client connection
System.out.println("Waiting for connection");
Socket incoming = s.accept( );
try
{
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
DataInputStream dataIn = new DataInputStream(inStream);
System.out.println(dataIn);
//Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
out.println( "Hello! Enter BYE to exit." );
// echo client input
boolean done = false;
while (!done)
{
int x= dataIn.readInt();
// Jeg får en fejl her???
String line ="1";
if (x==1){
out.println("Echo: " + line);}
}
}
finally
{
incoming.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}