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);
}
}
}
} Short correction and a concrete approach: a TCP socket can absolutely carry a file — FTP is just an application protocol implemented on sockets. is incorrect here and is right to object. For the simplest reliable pattern is: client opens the file, sends a small header (filename and length), then streams the raw bytes. The server reads the header, streams the incoming bytes to disk (or into memory for small files), then decodes the saved bytes as text to display.
Example (minimal, single-connection flow):
/* Server side */
ServerSocket ss = new ServerSocket(12345);
try (Socket s = ss.accept();
DataInputStream in = new DataInputStream(s.getInputStream())) {
String name = in.readUTF();
long len = in.readLong();
try (FileOutputStream fos = new FileOutputStream("received_" + name)) {
byte[] buf = new byte[4096];
long rem = len;
while (rem > 0) {
int r = in.read(buf, 0, (int)Math.min(buf.length, rem));
if (r < 0) break;
fos.write(buf, 0, r);
rem -= r;
}
}
String text = new String(Files.readAllBytes(Paths.get("received_" + name)), StandardCharsets.UTF_8);
System.out.println(text);
} /* Client side */
try (Socket s = new Socket("server.host", 12345);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
FileInputStream fis = new FileInputStream("message.txt")) {
File f = new File("message.txt");
out.writeUTF(f.getName());
out.writeLong(f.length());
byte[] buf = new byte[4096];
int r;
while ((r = fis.read(buf)) != -1) out.write(buf, 0, r);
out.flush();
} Practical tips: run the server before connecting the client; use try-with-resources and handle IOExceptions; stream data (do not load large files wholly into memory); always send a length or use a framing protocol so the receiver knows where the file ends; handle character encoding explicitly when printing text (e.g., UTF-8). For multiple concurrent clients accept sockets in a loop and hand each to a worker thread or executor. For background reading on sockets in Java see the Java Sockets Tutorial.
Jump to Post— jwenting 1,905no, 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.
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.