?
Solved

help : file transfer using sockets???

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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