Results 1 to 2 of 2
- 12-02-2010, 08:38 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 25
- Rep Power
- 0
Sending and splitting an image file over tcp socket.
I am trying to write a server and client, where the server has an image file (hardcoded name), that is to be split into 512 byte packets and sent to a client who responds to each packet with an "ACK" message. The client is supposed to assemble these packets into an image file. Currently the code I have is giving problems, I don't now how to modify it.
The size of the image is 3092 bytes meaning the server should send about 6 packets and print "ack received" if the packet was received and acknowledged by the client, but it seems to only be printing it twice.
server code contains
client code containsJava Code:public static void main(String[] args) throws Exception { ServerSocket inSocket = new ServerSocket(40075); System.out.println("made socket"); String clientSentence; while(true) { Socket sSocket = inSocket.accept(); System.out.println("accepted socket"); BufferedReader fromclient = new BufferedReader(new InputStreamReader(sSocket.getInputStream())); DataOutputStream toClient = new DataOutputStream(sSocket.getOutputStream()); clientSentence = fromclient.readLine(); System.out.println(clientSentence); File jfile = new File("base.jpg"); if (!jfile.exists() || !jfile.isFile()) { System.out.println("Not a file"); } else System.out.println("Is a file"); int filesize = (int)jfile.length(); int left = filesize; int byteposition = 0; System.out.println(filesize); DataInputStream fis = new DataInputStream(new FileInputStream(jfile)); byte[] buffer = new byte[filesize]; while(left > 512) { try { while(fis.read(buffer)!=-1) { toClient.write(buffer, byteposition, 512); } System.out.println("After writing"); clientSentence = fromclient.readLine(); if (clientSentence.equals("ACK")); System.out.println("ack received"); } catch (Exception e){} byteposition = byteposition + 512; left = left - 512; } } }
Java Code:System.out.println("pre server socket"); Socket clSocket = new Socket(addr,40075); System.out.println("post server socket"); DataOutputStream toPserver = new DataOutputStream(clSocket.getOutputStream()); DataInputStream fromPserver = new DataInputStream(clSocket.getInputStream()); toPserver.writeBytes(filereq); String pReply = fromPserver.readLine(); System.out.println(pReply); File dlfile = new File("new"+name); dlfile.createNewFile(); int gotbytes = 0; DataOutputStream dos = new DataOutputStream(new FileOutputStream(dlfile)); while(gotbytes < 3092) { try { while(fromPserver.available() > 0) { dos.writeByte(fromPserver.readByte()); gotbytes = gotbytes + 1; } } catch (Exception e){} toPserver.writeBytes("ACK"); } System.out.println(gotbytes); clSocket.close(); }Last edited by busdude; 12-02-2010 at 08:43 AM.
- 12-02-2010, 10:03 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
Problems with sending zip file using Socket
By morita in forum NetworkingReplies: 0Last Post: 05-14-2010, 06:41 PM -
sending file from client to server - socket closed error
By forex in forum NetworkingReplies: 3Last Post: 04-05-2010, 02:19 AM -
socket communication between c++/java and sending image
By mdemir10 in forum NetworkingReplies: 2Last Post: 09-10-2009, 02:29 PM -
Sending a file through socket
By sureshkumarcs88 in forum NetworkingReplies: 2Last Post: 03-14-2009, 07:32 AM -
Sending a .Doc file from client to a server through socket
By amita_k29 in forum NetworkingReplies: 1Last Post: 02-10-2009, 09:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks