client/server
Vi sidder her og prøver at få noget data (en fil) sendt fra client til server. Til dette anvender vi FileOutputStream/FileInputStream sammen med BufferedReader/-Writer.Vi har socket connection, og er i stand til at kommunikere med f.eks. read-/writeUTF.
Sovsen følger her:
/****************\\
* Fserver.java *
\\****************/
import java.io.*;
import java.net.*;
public class Fserver
{
public static void main(String[] args) throws IOException
{
ServerSocket fserversocket = null;
Socket fsocket = null;
int sum = 0;
String sumString = \"\";
//create socket
try
{
fserversocket = new ServerSocket(1337);
}
catch (Exception e)
{
System.err.println(\"Exception Creating Socket\");
}
while(true)
{
try
{
//wait for connection then create streams
System.out.println(\"Waiting for socket connection\");
fsocket = fserversocket.accept();
//create streams
DataOutputStream dosstream = new DataOutputStream(new BufferedOutputStream(fsocket.getOutputStream()));
DataInputStream disstream = new DataInputStream(new BufferedInputStream(fsocket.getInputStream()));
/* Problems with this section
File testfile = new File(\"enfiljegharlavet.dat\");
FileOutputStream fosstream = new FileOutputStream(testfile);
BufferedOutputStream bos = new BufferedOutputStream(fosstream);
bos.flush();
bos.close();
*/
//execute client requests
while(true)
{
String myOperation = disstream.readUTF();
//perform increment function
if(myOperation.equals(\"increment\"))
sumString = String.valueOf(++sum);
//perform set sum operation
else
if(myOperation.equals(\"set_sum\"))
{
sum = 0;
sumString = String.valueOf(sum);
System.out.println(\"Sum = \" + sumString);
}
dosstream.writeUTF(sumString);
dosstream.flush();
}
}
catch (Exception e)
{
System.err.println(\"Closing Socket Connection\");
if(fsocket != null)
try
{
fsocket.close();
}
catch (IOException ex)
{}
}
}
}
}
/****************\\
* Fclient.java *
\\****************/
import java.io.*;
import java.net.*;
public class Fclient
{
public static void main(String[] args)
{
String sumString = \"\";
try
{
if(args.length != 0)
{
System.out.println(\"Usage java Fclient <host> <count>\");
return;
}
//create socket connection
System.out.println(\"Opening socket and creating streams\");
/* String host = args[0] */
String host = \"localhost\";
Socket socket = new Socket(host, 1337);
//create streams
DataOutputStream dosstream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
DataInputStream disstream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String file = \"test2.dat\";
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
/* Problems with this section
File testfile = new File(\"test2.dat\");
FileInputStream fisstream = new FileInputStream(testfile);
BufferedInputStream bis = new BufferedInputStream(fisstream);
*/
//set sum to aero
System.out.println(\"Setting sums to zero\");
dosstream.writeUTF(\"set_sum\");
dosstream.flush();
sumString = disstream.readUTF();
//get count, initialize start time
System.out.println(\"INCREMENTING...PLZ STAND BY...\");
// int count = new Integer(args[1]).intValue();
int temp = 100;
int count = temp;
long startTime = System.currentTimeMillis();
//perform increment \"count\" number of times
for(int i = 0; i < count; i++)
{
dosstream.writeUTF(\"increment\");
dosstream.flush();
sumString = disstream.readUTF();
}
//display stat
long stopTime = System.currentTimeMillis();
System.out.println(\"Average ping = \" + ((stopTime - startTime)/(float)count) + \" msecs\");
System.out.println(\"Sum = \" + sumString);
/*
bis.close();
*/
}
catch(Exception e)
{
System.err.println(e);
}
}
}
På forhånd tak :)
