7 Replies - 721 Views - Last Post: 07 September 2012 - 09:30 PM Rate Topic: -----

#1 KasunL  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 11-April 11

Sending an MS Access file via Sockets to the server?

Posted 06 September 2012 - 02:50 AM

Hi!

i tried sending a text file from client to server, which the server recognized as a text string and displayed it's contents. Now, is it possible to send a non textual file as a MS Access file from client to server via sockets, maybe using the the SendFile() method? i need to send the file and store it on the server.

Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Sending an MS Access file via Sockets to the server?

#2 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Sending an MS Access file via Sockets to the server?

Posted 06 September 2012 - 04:52 AM

are the server and the client on the same network? if so, would it not be easier to just copy the file there with a UNC path?

Have you tried to send the file over the socket to see what happens?
Was This Post Helpful? 0
  • +
  • -

#3 KasunL  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 11-April 11

Re: Sending an MS Access file via Sockets to the server?

Posted 06 September 2012 - 08:29 AM

View PostBigR1983, on 06 September 2012 - 05:52 PM, said:

are the server and the client on the same network? if so, would it not be easier to just copy the file there with a UNC path?


No, the file should be transmitted to the server via the Internet.

View PostBigR1983, on 06 September 2012 - 05:52 PM, said:

Have you tried to send the file over the socket to see what happens?


Yes. When i send a text file, the server recognizes the text contents in the file. Because currently it looks for text data. So when i send an Access file, the client just freezes. i donno how to receive non-text files with sockets :helpsmilie:

This post has been edited by KasunL: 06 September 2012 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,971
  • Joined: 08-June 10

Re: Sending an MS Access file via Sockets to the server?

Posted 06 September 2012 - 08:52 AM

You shouldn't read the file as text. You're going to send it over the socket as an array of bytes anyway, right? There's a static method on the File class that will read the contents of a file as a byte array (called ReadAllBytes).

Also, if the client is freezing (not crashing) that means the operation hasn't completed yet. How big is the Access file? Even if you do it correctly, it might take a long time to read the entire file into memory and send it. If it's too big, you might have to implement some mechanism to read in chunks, send, then read, then send, until you're done. That'd also require some effort on the server side.

If this is all you'll send over the socket, you can just write those bytes to a file on the other side. If not, you'll have to send over some other data as a way to identify the name and type of file that's coming across.
Was This Post Helpful? 0
  • +
  • -

#5 KasunL  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 11-April 11

Re: Sending an MS Access file via Sockets to the server?

Posted 06 September 2012 - 06:47 PM

Ok i've tried WriteAllBytes() to save the received bytes to the disk. The file is around 1.5 Mbs, and the testing server is on localhost. When i attempt to transfer file, the client gets freezed and the sent file is written to disk as a 8kb file(which is originally 1.5Mb). When i debug the server, the received bytes also read as around 8000 bytes. This is my code:


At the Server:

while (true)
{
 sckRecv.Listen(100);
 accepted = sckRecv.Accept();

 Buffer = new byte[accepted.SendBufferSize];
 int bytesRead = accepted.Receive(Buffer); // Read as around 8000 bytes
 File.WriteAllBytes("MARITIMEDB_RECIEVED.accdb", Buffer);
}





At the Client:

..
Connect();
sckSend.SendFile("MARITIMEDB_BACKUP.accdb");
..




Why is the file not transferred correctly?
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,971
  • Joined: 08-June 10

Re: Sending an MS Access file via Sockets to the server?

Posted 06 September 2012 - 08:02 PM

Let me just ask this...do you have to do this with sockets? There's not a whole lot of reasons for you to create a socket server and client just to send a file. WFC will let you upload a file quite easily.
Was This Post Helpful? 0
  • +
  • -

#7 KasunL  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 11-April 11

Re: Sending an MS Access file via Sockets to the server?

Posted 07 September 2012 - 07:13 PM

The app is written as a C# windows forms app. Tried below code too, but still with no luck of sending the 1.5Mb file. only gets transferred is a small potion of it. Code at server (local testing):


..

IPAddress myAddress = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(myAddress, 1234);
listener.Start();
TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
byte[] buffer = new byte[500000];

MemoryStream ms = new MemoryStream();
int length = 0;

do
{

 length = ns.Read(buffer, 0, buffer.Length);
 ms.Write(buffer, 0, length);

} while (length > 0);

File.WriteAllBytes("MARITIMEDB_RECIEVED.accdb", buffer);

..


This post has been edited by KasunL: 07 September 2012 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3646
  • View blog
  • Posts: 11,412
  • Joined: 05-May 12

Re: Sending an MS Access file via Sockets to the server?

Posted 07 September 2012 - 09:30 PM

Your line 21 only writes out the buffer. What did you expect it to do? Magically reach into the MemoryStream that you created and write the data that was written into that? So far, computers will only do what you tell them to do. We haven't developed mind reading compilers yet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1