Hi I want to implement a program where
client sends a file to server and server displays the message from the file.
I have attatched my java files for reference
Hi I want to implement a program where
client sends a file to server and server displays the message from the file.
I have attatched my java files for reference
// Client program
import java.io.*;
import java.net.*;
public class TcpClient
{
private static InetAddress host;
File f1;
public static void main(String[] args)
{
try {
// Get server IP-address
host = InetAddress.getByName(args[0]);
}
catch(UnknownHostException e){
System.out.println("Host ID not found!");
System.exit(1);
}
run(Integer.parseInt(args[1]));
}
private static void run(int port)
{
Socket link = null;
try{
// Establish a connection to the server
link = new Socket(host,port);
// Set up input and output streams for the connection
BufferedReader in = new BufferedReader(
new InputStreamReader(link.getInputStream()));
PrintWriter out = new PrintWriter(
link.getOutputStream(),true);
//Set up stream for keyboard entry
BufferedReader userEntry = new BufferedReader(
new InputStreamReader(System.in));
String message, response;
// Get data from the user and send it to the server
do{
System.out.print("Enter message: ");
message = userEntry.readLine();
out.println(message);
}while (!message.equals("DONE"));
// Receive the final report and close the connection
response = in.readLine();
System.out.println(response);
}
catch(IOException e){
e.printStackTrace();
}
finally{
try{
System.out.println("\n!!!!! Closing connection... !!!!!");
link.close();
}
catch(IOException e){
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}
// Server program
import java.io.*;
import java.net.*;
public class TcpServer
{
private static ServerSocket servSock;
public static void main(String[] args)
{
System.out.println("Opening port...\n");
try{
// Create a server object
servSock = new ServerSocket(Integer.parseInt(args[0]));
}
catch(IOException e){
System.out.println("Unable to attach to port!");
System.exit(1);
}
do
{
run();
}while (true);
}
private static void run()
{
Socket link = null;
try{
// Put the server into a waiting state
link = servSock.accept();
// Set up input and output streams for socket
BufferedReader in =
new BufferedReader(
new InputStreamReader(link.getInputStream()));
PrintWriter out = new PrintWriter(link.getOutputStream(),true);
// print local host name
String host = InetAddress.getLocalHost().getHostName();
System.out.println("Client has estabished a connection to " + host);
// Receive and process the incoming data
int numMessages = 0;
String message = in.readLine();
//String message1 = in.FileReader();
String Record = br.readLine();
while (!message.equals("DONE"))
{
System.out.println(message);
numMessages++;
message = in.readLine();
}
// Send a report back and close the connection
out.println("Server received " + numMessages + " messages");
}
catch(IOException e){
e.printStackTrace();
}
finally{
try{
System.out.println("!!!!! Closing connection... !!!!!\n" +
"!!! Waiting for the next connection... !!!");
link.close();
}
catch(IOException e){
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}
no, we're not going to open attachments. And we're not going to do your homework for you either.
We also can't guess at what your question in, as you're not asking any.
Use FTP rather, you cannot send a file over a socket.
... you cannot send a file over a socket.
Since when? How do you think FTP does it?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.