Solved

help : file transfer using sockets???

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now