Client and Server Sockets For File Uploads

shelal

Member
Joined
Jul 6, 2006
Messages
10
Programming Experience
3-5
Hi all
i attchached a file of client and server socket project i found on the net.
it works successfully if the file size is less than or equal 8 byte and image copied.
but if the size was greater than that the file copied but it will be coruppted.
can anyone help on how to make it works for larger files???
any comment will be very helpfull
thanks
shelal
 

Attachments

  • NetworkingVB.zip
    79 KB · Views: 80
The client-server in attached projects works for any filesize, but they always writes to same file "test.bmp" with FileMode.Append. So if you send one file then send another it will be appended and those two files combined is nonsense. You see the code in INPServer class in OnDataRecieved method. I also found one problem in INPClient class, that the filestream isn't closed so client won't release a file it sends until application closes. Other than that it looks like something that enables you to build from.
 
BUFFERSIZE is Constant

thank you for your reply,,sorry to be late, but we have vacation here and i couldn't reach my pc before now...
when i tried the project, i found it worked only if the file sie is less than or equal 1024. since BUFFERSIZE is constant and it's value equal 1024 in the INPServer and INPClient classes..
do you have any idea how to change this value depending on the user selected file he wants to send.

thanks.
 
That is not true. The classes is working as they should, using a read buffer is how it normally works. And for isolated transfers the received buffers must be appended like they currently are.

What's missing, aka what you have to build yourself, is the logic layer to handle separate transfers, ie most commonly create some kind of header information to know what is sent so that the receiver knows how to handle it appropriately.

At the extreme minimum just to get some basic understanding of the byte/buffer transfer mechanism, you can add a filename string property to the INPServer class, and in frmServer you change that filename (edit: change with the instance of INPServer _server.filename, of course) each time ReceiveCompleted event is raised - if you manage to do that you will see that each file client sends now ends up in separate files on server.
 
Last edited:
Hi JohnH
i tried what you said, i added fileName property to INPServer class and i changed it in frmServer in the _server_ClientConnected event
since it is not taking it if i put it in the server_RecieveCompleted event
the file created with the specified name, but it seems that the server hangs
and the image is created but with mixed color and some parts appear and the others not!!!
Plz be patient with me and help me plz, i am realy confused and i need to solve this issue as soon as possible!!!
thanks


 
Well, there really isn't more to it than what I already have said.

Using client connected event is bad for changing filename because client can connect one time and send multiple files.

Mind you, this filename talk here can only be a temporary solution to help you get going, perhaps it is not helping you at all. You see, it does matter what you name a file and only client can tell server what file it is sending. So setting just any filename in server could be trouble. Say if client sends a zip file and server name it 'temp.bmp', when you open this file Windows will try to display it as a bitmap image, which doesn't work because it really is a zip file, see?

Anyway, to get you going with multiple file transfers, do as I have said and change the filename in RecieveCompleted event, it's the only event that can tell you when one transfer has ended (and a new one may begin). I can assure you again that the send-receive classes are fully functional. (With the comment above about fs.close easily fixed, but not an issue for your current problems.)

Your problem with this could be that you haven't set a filename for the first transfer, perhaps? Remember, RecieveCompleted event happens for the first time after the first transfer.
 
file name null

but when i change the file name property in the _server_RecieveCompleted in frmServer form, it gives me an exception SystemArgumentNullException saying path can not be null
on the sentence
VB.NET:
Dim fs As FileStream = New FileStream(strFileName, FileMode.Append)

where strFileName the a private member variable used for the file name proprty

 
Last edited by a moderator:
What if you just declared in INPServer class:
VB.NET:
Public filename As String
And whenever you want to set a filename i frmServer you simple do:
VB.NET:
_server.filename = "somefilename"
 
Socket Server Problem

Hi JohnH
Sorry for bothering you with my questions.
i tried the server/client sample solution and i faced some problems. i will be gratefull if you can help me in this issue.
i tried to run the server on one machine and two clients, one from the same machine and the other on another one(the two clients send at the same time).
i noticed the following:-
1- the server can recieve from one client only that on the other machine.
the other case i tested, server and client on the same machine,first time the client send a file, the server recieve it,but if i closed this client and opened another and the server still opened, it can not recieve more files.

thanks in advanced
shelal
 
Yes, it is client/server example, not clients/server example. It was done simple for beginner to understand and get started. Less than 50 lines of code each project doesn't do much for advanced features.

If you want server to handle multiple clients it is not very difficult, you just have to set up a separate listening thread that accepts a client and then loops to accept another one.

Another thing you must implement for the server with several clients is port handling. A port can only be used by one session at the same time. That means you must have an exclusive listening service port and for each client that connects server sends back a free communication port that the client will connect to for communication, server will in the mean time add the client session to its list and set up a new listener at the allocated port. This is the only way to go about allowing multiple clients to connect and communicate at the same time.

Each client accepted for communication should be handled in its own separate client thread. Depending on how you want communication to take place you may want to put all the accepted clients in an array or collection.

Serverside you may decide for yourself what is the best suited way to set up the communication level methods, whether you want to encapsulate them in a class that you make an instance of for each client, or if you will use asynchronous delegates at main class and invoke when necessary. The first is better with long running sessions when much data is sent back and forth, the other easier with one way communication.

It's all up to you, you define the protocol yourself for the specialized communication that takes place between the client and server.
 
help for a biggener

Hi JohnH,
sorry for late in the reply, but we have 2 days vacation here and i couldn't reach my pc before now.
i want to ask for your help with some code snippet about you are suggest...
i am new to the Socket issue and i don't have any experience in it nor in the Threading.
i read articles about how to implement Asychronous Server and Client Sockets in the online MSDN on the following link http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnon-blockingserversocketexample.asp
but it seems they are using different techniques.
sorry for bothering you, it will be very appreciated if you can post anything.
shelal
 
Port Number

hi JohnH
first of all, thank you for your post, it is very appreciated.
i tried your sample and i have one question more;
i noticed that i can not use the same port number from two different clients to connect to the server and send data at the same time, right or not?
and is it a socket issue rule or what?
thanks in advanced
shelal
 
Back
Top