?
Solved

help : file transfer using sockets???

Posted on 2003-11-22
3
Medium Priority
?
739 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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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