Results 1 to 2 of 2
Thread: Client Server Communication
- 11-04-2012, 07:11 PM #1
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
Client Server Communication
Hello,
I have a Server and a Client. The Client tries to receive a file from the Server. The Server sends the content of the File and a header. My Client works fine but I have a problem with my Server.
I start the Server with> java Server <port>
And the Client with> java Client <name of Server> <port> <file>
That's my Server:
Java Code:import java.io.*; import java.net.*; public class Server { private int port; private String type = "txt"; public Server(int port) { this.port = port; } public void startServer() { try { ServerSocket server = new ServerSocket(port); InetAddress addr = InetAddress.getLocalHost(); System.out.println("HTTPServer " + addr.getHostName() + "/" + addr.getHostAddress() + ":" + port + " started ..."); while (true) { Socket client = server.accept(); File file = new File("data.txt"); byte[] bytearray = new byte [(int)file.length()]; BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream()); FileInputStream inputStream = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(inputStream); bis.read(bytearray,0, bytearray.length); OutputStream outputStream = client.getOutputStream(); try { if(file.exists()) { String header = "HTTP/1.1 200 OK\r\n" + "Content-Type: " + type + "\r\n" + "Content-Length: " + file.length() + "\r\n\r\n"; outputStream.write(header.getBytes()); outputStream.write(bytearray,0,bytearray.length); } else (!file.exists()) { String header = "HTTP/1.1 404 Bad Request\r\n" + "Content-Type: " + type + "\r\n" + "Content-Length: " + file.length() + "\r\n\r\n"; outputStream.write(header.getBytes()); } out.flush(); out.close(); } catch(IOException e) { System.err.println(e); } finally { try { if(client != null) client.close(); if(inputStream != null) inputStream.close(); } catch(IOException e) {} } } } catch(IOException e) { System.err.println(e); } } public static void main(String[] args) { if(args.length != 1) { System.err.println("HTTPServer <port>"); System.exit(1); } int port = Integer.parseInt(args[0]); new Server(port).startServer(); } }
Java Code:import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws UnknownHostException, IOException { String serverName = new String(args[0]); int port = Integer.parseInt(args[1]); String file = args[2]; Socket s = new Socket(serverName, port); OutputStream os = s.getOutputStream(); PrintWriter pw = new PrintWriter(os, false); pw.println("GET " + "http://" + s.getInetAddress() + file + " HTTP/1.1" + "\n"); pw.println(); pw.flush(); InputStream in = s.getInputStream(); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } }
Thank you for your help!Last edited by MichaelH; 11-04-2012 at 07:48 PM.
- 11-09-2012, 08:40 AM #2
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Receive multiple vectors (communication client-server)
By shmatty52 in forum NetworkingReplies: 0Last Post: 07-16-2012, 08:44 PM -
Use Servlet for client to client communication?
By ChexWithRaisins in forum Java ServletReplies: 0Last Post: 08-03-2011, 06:29 AM -
Client to Client communication
By ja107 in forum NetworkingReplies: 6Last Post: 10-09-2010, 03:52 AM -
server-client; client sends a username to the server.
By lkcz in forum New To JavaReplies: 2Last Post: 09-24-2010, 12:31 PM -
client-server communication problem
By revathi17 in forum New To JavaReplies: 1Last Post: 08-09-2007, 03:21 PM
Bookmarks