Solved

help : file transfer using sockets???

Posted on 2003-11-22
3
734 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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

740 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