This week's giveaway is in the Java in General forum.
We're giving away four copies of Java Challengers and have Rafael del Nero on-line!
See this thread for details.
Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

how to send a file or photo to clients using sockets??

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI
I want to know how the clients can transfer a file or photo to one another when connected using sockets in java networking.Can any one help me how this should be done.It should work in a similar way as in msn messenger.Pls help me.
Thanks in Advance
---Yegappan---
vv
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
the way you present this to the client is more related to GUI. you can create a ServerSocket and accept incomming connections. on the other side you can create a normal socket and connect to this socket. then you can get the input and outputstreams from these sockets. there you can read and write whatever you want.....
i guess you can find tips in the network forum.
pascal
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should do something like this for the source socket and the target socket, the source socket is the one whose owns the file ( or photo ) and the target socket is the one whose recive it.
Source
1.- Get and output stream for the source ( from the target )
2.- read the file using an input stream
3.- wirte it out to the output stream
4.- close the file
5.- close the stream
Target
1.- get notified somehow that a file is coming thruogh the socket
2.- open an input stream for the target ( from the source )
3.- open an output stream for a file..
4.- write the content to the file
5.- close the file , and close the input stream.
Notice that I have naver done this before, but I guess It will work
try it if you can't Ill can give you some code ( that I don't have tough :P )
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yegappan,
Pascal is perfectly right. There's more than just networking in this application. But networking is still a very big part.
A basic design could be to have on application running on a server to which all users will connect. We could call this server a directory. This directory will listen (using ServerSocket) on a specific port, e.g. 21012 and all client application will connect to its IP adress using port 21012 (using regular Sockets). For each client connection aquired, the server will create a new thread (or wait for an available thread from a pool of threads) to handle the connection.
The client application and the server application will then communicate on this socket connection. The text they exchange is called a protocol. You have to design the grammar of this protocol to support the services you want to offer. For instance you could use XML to express your commands:
To login, the client sends:
<code><login user="username" password="password" /></code>
In case of success the server will reply
<code><success /></code>
In case of failure it will reply
<code><error>Invalid user of password</error></code>
You should include commands to register to the service, identify, change state (online, busy, etc), get IP address and port of another user for initiating chat sessions, etc...
Then, each client must also have a ServerSocket listening on a specific port to handle chat session.
Let's say user Alfred is running on client A and Brutus is running client B. When Alfred want to chat with Brutus, A will ask the directory for connection parameters to connect to Brutus. The directory will reply with parameters for client B. Client A will then open a new socket connection to connect to client B. They will use another protocol designed to handle chat session and file exchanges (you can create a basic protocol, similar to the one you created with the directory).
For instance,
to send text:
<code><text>bla bla bla</text></code>
To handle file exchange, it's best to use another separate socket connection to allow people to continue chating file the file transfer occur. For instance you may use the following senario to send a file:
On the chat socket, client A sends:
<code><file name="Holidays in Florida.gif" bytes="1253456" /></code>
if user accepts to receive this file, client B creates a new ServerSocket on an available port and replies by giving this port number to client B:
<code><accept port="21014"</code>
Then client B connects to client A on the given port and sends the bytes for the files.
Client A receives those bytes and saves them to a file using the previously given filename.
Most of this can be done easily if you have a good understanding of socket programming and of multi-threading.
Do not forget that many companies are using firewalls and the ports will choose will be forbiden in most cases. Hence, document the port used by your application so that the users of your software can ask their network administrator to allow these ports to be used.
Hope these ideas can help you to write your application.
Beno�t
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very complete answer of Beno�t d'Oncieu. but because you crossposted your question in different forums not everybody will read it.......so only post a question in one forum.
for the "language" the apps use to communicate you could use soap , i think. it is xml based and you could save some effort.

pascal
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using SOAP for this exchange would be like to use a sledgehammer to crack a wallnut and I honnestly don't think you would save time using it...
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can just create a socket by using Datagram socket or Stream socket. Then open the output stream at the client side, open the file inputsteam(you files or photo), write the file byte by byte into the socket output stream. Then that's done.
David hu
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a very simple requirement and designing a complex protocol will be too much of an effort unless you have plans to expand this further.
I would suggest this strategy/protocol
1. Client begins handshake by sending a String.(You can call this login)
2. Server acknowledges by sending a known stream of characters.
3. Client asks for a list of available files
4. Server sends back the list
5. Client requests for a specific file using an id
6. Server sends back the data for the file over the socket
7. Steps 5 and 6 repeated n times
8. Client ends handshake by sending another string(logout).
9. Server closes socket.
If you really are keen to go for a standard protocol then I would suggest FTP

------------------
Shubhrajit
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

benoit: i like sledgehammers :-) but your right.
for just sending files over the net one wouldnt/shouldnt use soap. then again: creating a tool to send files over the net sounds like a study project (since there are tons of apps doing this, including eMail, icq, aim, and all the p2p apps).
so it could be interesting to use soap but i agree, it's too heavy.
pascal
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic