Solved

help : file transfer using sockets???

Posted on 2003-11-22
3
735 Views
Last Modified: 2006-11-17
can someone explain me how i cud transfer files from client to server & from server to client using tcp sockets.

if any1 has worked on the same thing ... cud u tell me how u did it ..

also if assuming both my client and server are on same machine and i have just one card ... i will have just one ip address ... so if i require a server to be connected to random number of clients ... how wud i do it ... i know port numbers cud be used .. but without hardcoding the port numbers ... how wud i assign port numbers for each connection?

thanks a lot

aseem
0
Comment
Question by:aseemlalani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
wolfc earned 125 total points
ID: 9802247
The examples are not made for speed, but are working prototypes. If you need more concurrency on the server use threading or non blocking io.

The server:

package test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerTest
{
      public static void write(InputStream in, OutputStream out)
            throws IOException
      {
            int c;
            while((c = in.read()) != -1)
            {
                  out.write(c);
            }
      }
      
      public static void main(String[] args)
      {
            try
            {
                  ServerSocket socket = new ServerSocket(4500);
                  while(true)
                  {
                        Socket client = socket.accept();
                        System.out.println("client = " + client);
                        FileInputStream fin = new FileInputStream("a.txt");
                        write(fin, client.getOutputStream());
                        fin.close();
                        client.close();
                  }
            }
            catch(Throwable t)
            {
                  t.printStackTrace();
            }
      }
}

and the client:

package test;

import java.net.Socket;

public class ClientTest
{
      public static void main(String[] args)
      {
            try
            {
                  Socket socket = new Socket("localhost", 4500);
                  ServerTest.write(socket.getInputStream(), System.out);
                  System.out.flush();
                  socket.close();
            }
            catch(Throwable t)
            {
                  t.printStackTrace();
            }
      }
}

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question