TCP Client/Server program getting stuck when - java

I have been working on creating a program where communication can be exchanged between a server and a client using to program files in java. However, when I try to send messages from server to client. I have been working on a project where I am trying to send a message from a server to a client. However, when I put in my message, from the server, the client seems to get stuck on sentence = inFromServer.readLine(); in the client portion of my code and I'm not sure why. I ran the debugger on this, and it seems that "sentence" takes in the value that was inputted from the client, so im not sure why it get stuck. Does anyone have an idea what the problem may be?
Here's my code for reference-
Server Code:
package com.company;
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.concurrent.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
class threading implements Runnable {
private int portnumbers;
Socket clientsocket;
ServerSocket connectorsocket;
public threading(Socket clientsocket)
{
this.clientsocket = clientsocket;
}
public void run(){
try {
Serverstuff(this.clientsocket);
}
catch(Exception ex)
{
System.out.print("I found exception" + ex);
}
}
public void Serverstuff(Socket socketlol)
{
// File h = null;
int width = 1536;
int height = 2048;
BufferedImage image = null;
File f = null;
while(true) {
try {
String sentence1;
//Wait on welcoming socket for client
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(socketlol.getInputStream()));
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream outToClient = new DataOutputStream(socketlol.getOutputStream());//create input stream, attached to socket
System.out.println("Flag1");
sentence1 = inFromUser.readLine();
System.out.println("Flag2");
System.out.println("Flag3");
outToClient.writeBytes(sentence1);
}
catch(Exception ex)
{
System.out.print("I found exception" + ex);
}
}
}
}
public class server{
public static void main(String argv[]) throws Exception {
String clientSentence;
String capitalizedSentence;
int portnumber;
Scanner scanney = new Scanner(System.in);
System.out.println("Enter the number of clients");
String arg1 = scanney.next();
int arg2 = Integer.parseInt(arg1);
int[] socketarray = new int[arg2];
for(int i = 0; i < arg2; i++) {
System.out.println("enter the port number");
String portnumberstr = scanney.next();
portnumber = Integer.parseInt(portnumberstr);
socketarray[i] = portnumber;
}
//ServerSocket welcomeSocket = new ServerSocket(5000);
int g = 0;
int whatever;
//ServerSocket welcomeSocket2 = new ServerSocket(5001);
while (true) {
if(g < arg2) {
System.out.println("Flagwhile1");
whatever = socketarray[g];
ServerSocket welcomeSocket = new ServerSocket(whatever);
System.out.println("Flagwhile2");
Socket clientsock = welcomeSocket.accept();
threading newthread = new threading(clientsock);
System.out.println("Flagwhile3");
new Thread(newthread).start();
g = g + 1;
}
}
}
}
Client Code:
package com.company;
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.concurrent.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class client2{
public static void main(String argv[]) throws Exception {
try {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); //Create input stream
Socket clientSocket = new Socket("127.0.0.2", 5001); //Create client socket connect to server
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); //Create output stream, attached to socket
System.out.println("Flag19");
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); //Create input stream attached to socket
System.out.println("Flag20");
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n'); //send line to server
System.out.println("Flag21");
sentence = inFromServer.readLine();
System.out.println("Flag22");
System.out.println(sentence);
int test = 5;
clientSocket.close();
}
catch(IOException e){
System.out.println("Error: " + e);
}
}
}

Related

Thread Pooled Concurrent Server (JAVA)

This is my 2nd ever Post I'm pretty new to Programming.
I'm working on making my Socket Server Concurrent (Multithreaded) on the Server side. I understand I have to use Threaded Pools after my Socket accepts but I'm just very confused what to do. currently the requests are slower then it was when it was only iterative because its not working correctly.
//MultiClient Class
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.net.*;
import java.io.*;
public class MultiServer {
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
System.out.println("What port should the server be using?");
int portNumber = scnr.nextInt();
while ((portNumber < 1025) ||(portNumber > 4998)) {
System.out.println("Please enter a Port Number between '1025' and '4998'.");
portNumber = scnr.nextInt();
}
try (ServerSocket serverSock = new ServerSocket(portNumber)){
System.out.println("Server listening on port " + portNumber);
while(true) {
Socket sock = serverSock.accept();
MultiServerHandler msh = new MultiServerHandler(sock);
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
executor.execute(msh);
}
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}
//MultiClientHandler Class
import java.io.*;
import java.net.*;
public class MultiServerHandler extends Thread{
private Socket sock;
public MultiServerHandler(Socket sock) {
this.sock = sock;
}
public void run() {
try {
System.out.println("New client connected");
InputStream input = sock.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
OutputStream output = sock.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
String command;
command = reader.readLine();
Process process = Runtime.getRuntime().exec(command);
BufferedReader read = new BufferedReader(new InputStreamReader(process.getInputStream()));
String result;
while((result = read.readLine()) != null) {
writer.println(result);
}
sock.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
So far I just know I have to implement it after the socket accepts but I'm not too sure yet.

Multi Client Simple Chat(non-GUI) Server in Java using threads

I am unable to figure out how to stop the message from appearing twice on both the client's screen.
The Actual output should be something like this:
Steps for Running the code:
1. Run Server on one terminal
2. Run two clients on two different terminals
When I run the Server - main method creates a Server object:
public static void main(String[] args) throws IOException {
Server server = new Server();
}
Server Constructor:
Server() throws IOException {
Date dNow = new Date();
System.out.println("MultiThreadServer started at " + String.format("%tc", dNow));
System.out.println();
ServerSocket server = new ServerSocket(8000);
ClientSockets = new Vector<Socket>();
while (true) {
Socket client = server.accept();
AcceptClient acceptClient = new AcceptClient(client);
System.out.println("Connection from Socket " + "[addr = " + client.getLocalAddress() + ",port = "
+ client.getPort() + ",localport = " + client.getLocalPort() + "] at "
+ String.format("%tc", dNow));
System.out.println();
//System.out.println(clientCount);
}
//server.close();
}
I am using Socket to connect to the server. Here is my Server code.
Server.java
import java.io.IOException;
import java.net.*;
import java.util.Vector;
import java.io.*;
import java.util.*;
import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.text.*;
import java.util.Scanner;
public class Server {
static Vector<Socket> ClientSockets;
int clientCount = 0;
//int i = 0;
Server() throws IOException {
Date dNow = new Date();
System.out.println("MultiThreadServer started at " + String.format("%tc", dNow));
System.out.println();
ServerSocket server = new ServerSocket(8000);
ClientSockets = new Vector<Socket>();
while (true) {
Socket client = server.accept();
AcceptClient acceptClient = new AcceptClient(client);
System.out.println("Connection from Socket " + "[addr = " + client.getLocalAddress() + ",port = "
+ client.getPort() + ",localport = " + client.getLocalPort() + "] at "
+ String.format("%tc", dNow));
System.out.println();
//System.out.println(clientCount);
}
//server.close();
}
public static void main(String[] args) throws IOException {
Server server = new Server();
}
class AcceptClient extends Thread {
Socket ClientSocket;
DataInputStream din;
DataOutputStream dout;
AcceptClient(Socket client) throws IOException {
ClientSocket = client;
din = new DataInputStream(ClientSocket.getInputStream());
dout = new DataOutputStream(ClientSocket.getOutputStream());
//String LoginName = din.readUTF();
//i = clientCount;
clientCount++;
ClientSockets.add(ClientSocket);
//System.out.println(ClientSockets.elementAt(i));
//System.out.println(ClientSockets.elementAt(1));
start();
}
public void run() {
try {
while (true) {
String msgFromClient = din.readUTF();
System.out.println(msgFromClient);
for (int i = 0; i < ClientSockets.size(); i++) {
Socket pSocket = (Socket) ClientSockets.elementAt(i);
DataOutputStream pOut = new DataOutputStream(pSocket.getOutputStream());
pOut.writeUTF(msgFromClient);
pOut.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Client.java
import java.net.Socket;
import java.util.Scanner;
import java.io.*;
import java.net.*;
public class Client implements Runnable{
Socket socketConnection;
DataOutputStream outToServer;
DataInputStream din;
Client() throws UnknownHostException, IOException {
socketConnection = new Socket("127.0.0.1", 8000);
outToServer = new DataOutputStream(socketConnection.getOutputStream());
din = new DataInputStream(socketConnection.getInputStream());
Thread thread;
thread = new Thread(this);
thread.start();
BufferedReader br = null;
String ClientName = null;
Scanner input = new Scanner(System.in);
String SQL = "";
try {
System.out.print("Enter you name: ");
ClientName = input.next();
ClientName += ": ";
//QUERY PASSING
br = new BufferedReader(new InputStreamReader(System.in));
while (!SQL.equalsIgnoreCase("exit")) {
System.out.println();
System.out.print(ClientName);
SQL = br.readLine();
//SQL = input.next();
outToServer.writeUTF(ClientName + SQL);
//outToServer.flush();
//System.out.println(din.readUTF());
}
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] arg) throws UnknownHostException, IOException {
Client client = new Client();
}
public void run() {
while (true) {
try {
System.out.println("\n" + din.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The reason you have this is because you're sending the server whatever the client has written in the console, and the server sends it back to all of the clients (including the sender).
So you're writing a message in the console (and you see it) and then you're receiving it back as one of the clients (and you see it again).
A simple fix would be not to send the just received message back to the client (he already sees it in the console). Add this to the Server.AcceptClient#run method:
for (int i = 0; i < ClientSockets.size(); i++) {
Socket pSocket = (Socket) ClientSockets.elementAt(i);
if(ClientSocket.equals(pSocket)){
continue;
}
...

Client-Server communication in Java using Sockets

I am trying to write a simple and basic Java program where a client sends the server a string and the server is supposed to respond with a reversed string. I am sure I have the correct program structure and flow but my server is not read the string from my client. I have narrowed the problem to this line on the server side: string = inputStream.readLine();
Here is my code. What could be the problem?
Server1.java
import java.io.*;
import java.net.*;
class Server1 {
public static void main(String[] args) throws Exception {
String string = null;
ServerSocket myServerSocket = new ServerSocket(4000); //Create Socket
System.out.println("Server Running...");
Socket clientSocket = myServerSocket.accept();
DataInputStream inputStream = new DataInputStream(clientSocket.getInputStream());
PrintStream outputStream = new PrintStream(clientSocket.getOutputStream());
do {
string = inputStream.readLine();
if(string!=null){
//using StringBuilder method to reverse string
StringBuilder input = new StringBuilder();
// append a string into StringBuilder input1
input.append(input);
// reverse StringBuilder input1
input = input.reverse();
// print reversed String
for (int i = 0; i < input.length(); i++) {
outputStream.println(input.charAt(i));
}
}
} while (true);
/*outputStream.println("exit");
outputStream.close();
inputStream.close();
myServerSocket.close();
System.out.println("Server Closed!");*/
}
}
Client1.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
class Client1 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in); //Object to read keyboard input
String string = null, response = null; //Variable to store string
Socket mySocket = new Socket("127.0.0.1", 4000); //Create Socket
DataOutputStream outputStream = new DataOutputStream(mySocket.getOutputStream());
DataInputStream inputStream = new DataInputStream(mySocket.getInputStream());
System.out.println("Client Running...");
do {
System.out.println("Type in a string and Press Enter...");
string = sc.next();
outputStream.writeBytes(string);
response = inputStream.readLine();
if (response != null) {
System.out.println("Server Response: " + response);
}
} while (true);
}
}
The problem is that in this line string = inputStream.readLine();
it is searching for a line and if you wont add "\r\n" at the end of your massage it will keep searching for the lines end
I am trying to write a simple and basic Java program where a client
sends the server a string and the server is supposed to respond with a
reversed string. I am sure I have the correct program structure and
flow but my server is not read the string from my client. I have
narrowed the problem to this line on the server side: string =
inputStream.readLine(); Here is my code. What could be the problem?
Here, I can see copy paste mistake.
// append a string into StringBuilder input1
input.append(input);
Always use while(true) loop for reading message from the client,
Try below codes as for your question.
Server1.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server1 {
private static Socket socket;
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(4000);
System.out.println("Server Running...");
//Note: Server is running always. This is done using this while(true) loop
while (true) {
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String string = br.readLine();
System.out.println("Message received from client is " + string);
//Reverse string responce builder
try {
//using StringBuilder method to reverse string
StringBuilder input = new StringBuilder();
// append a string into StringBuilder input
input.append(string);
// reverse StringBuilder input
input = input.reverse();
string = input + "\n"; //Next to line
// print reversed String
for (int i = 0; i < input.length(); i++) {
System.out.println(input.charAt(i));
}
} catch (Exception e) {
//Invalid text message back to client.
string = "Please send a proper text message\n";
}
//Sending the response back to the client.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(string);
System.out.println("Message sent to the client is " + string);
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (Exception e) {
}
}
}
}
Client1.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client1 {
private static Socket socket;
public static void main(String args[]) {
try {
socket = new Socket("127.0.0.1", 4000);
System.out.println("Client Running...");
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
System.out.println("Type in a string and Press Enter...");
Scanner sc = new Scanner(System.in);
String string = sc.next();
System.out.println("string = " + string);
String sendMessage = string + "\n"; ////Next to line
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : " + sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " + message);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
//finally close the socket
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

“java.lang.UnsupportedOperationException: Not supported yet.”

I am making a TCP project in java.
I want to build a TCP connection in java with serial communication from a microcontroller.
I want to do that with Multithreading.
But when I run my server and client , and I send a message from my client to my server. Then I have the following error in the TCPserver :
"Exception in thread "Thread-0" java.lang.UnsupportedOperationException: Not supported yet.
at serialPort.openPort(serialPort.java:30)
at Client.run(TCPserver.java:63)"
Here is my TCPservercode :
import java.io.*;
import java.net.*;
import java.io.IOException;
import jssc.SerialPort;
import jssc.SerialPortException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import jssc.SerialPortList;
import jssc.*;
import jssc.SerialPort;
import jssc.SerialPortException;
class TCPServer
{
public static void main(String argv[]) throws Exception
{
ServerSocket welcomeSocket = new ServerSocket(6789);
SerialPort serialPort = new SerialPort("COM3");
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
if (connectionSocket != null)
{
Client client = new Client(connectionSocket);
client.start();
}
}
}
}
class Client extends Thread
{
private Socket connectionSocket;
private String clientSentence;
private String capitalizedSentence;
private BufferedReader inFromClient;
private DataOutputStream outToClient;
public Client(Socket c) throws IOException
{
connectionSocket = c;
}
public void run()
{
try
{
inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
serialPort.openPort();//Open serial port
serialPort.setParams(SerialPort.BAUDRATE_115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
System.out.println("Received: " + clientSentence);
serialPort.writeString( clientSentence + "\r\n");
//Thread.sleep(1000);
String buffer = serialPort.readString();//Read 10 bytes from seri
// Thread.sleep(1000);
outToClient.writeBytes(buffer);
System.out.println(buffer);
serialPort.closePort();
connectionSocket.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
and my TCPclient code :
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String argv[]) throws Exception
{
while(true){
String sentence;
String modifiedSentence;
Socket clientSocket = new Socket("localhost", 6789);
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
}
My original serial communication code :
package sensornode_Jens;
import java.io.IOException;
import jssc.SerialPort;
import jssc.SerialPortException;
import java.util.Scanner;
import jssc.SerialPortList;
import jssc.*;
import jssc.SerialPort; import jssc.SerialPortException;
public class Main {
public static void main(String[] args) {
// getting serial ports list into the array
String[] portNames = SerialPortList.getPortNames();
if (portNames.length == 0) {
System.out.println("There are no serial-ports :( You can use an emulator, such ad VSPE, to create a virtual serial port.");
System.out.println("Press Enter to exit...");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
return;
}
for (int i = 0; i < portNames.length; i++){
System.out.println(portNames[i]);
////
SerialPort serialPort = new SerialPort("/dev/ttyACM0");
try {
serialPort.openPort();//Open serial port
serialPort.setParams(SerialPort.BAUDRATE_115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
Scanner input = new Scanner(System.in);
String scommand = input.nextLine();
serialPort.writeString( scommand + "\r\n");
String buffer = serialPort.readString();//Read 10 bytes from serial port
System.out.println(buffer);
serialPort.closePort();
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
}
}
Can anybody help me?
Looking through the Google Code page for jssc on: https://code.google.com/archive/p/java-simple-serial-connector/
When reviewing the examples they use:
SerialPort serialPort = new SerialPort("COM1");
Which I see set in the TCPServer class but not in the Client class where
serialPort.openPort();
is called.
So you may need to initialize serialPort inside your Client class instead of your TCPServer class, before calling openPort on it.
I found no reference to the openPort method throwing a UnsupportedOperationException.

TCP socket communication failed after the first trial

I have received error message after the client side successful received one message from server side. The error message is: Exception in thread "main" java.net.SocketException: Software caused connection abort: recv failed
It seems in client class, line = inFromserver.readLine(); would not receive any message from server, making it become "null". But I dont know why. Could somebody please help me?
Server class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class ConcurrentServer {
public static void main(String args[]) throws IOException
{
int portNumber = 20020;
ServerSocket serverSocket = new ServerSocket(portNumber);
while ( true ) {
new ServerConnection(serverSocket.accept()).start();
}
}
}
class ServerConnection extends Thread
{
Socket clientSocket;
PrintWriter outToClient;
ServerConnection (Socket clientSocket) throws SocketException
{
this.clientSocket = clientSocket;
setPriority(NORM_PRIORITY - 1);
}
public void run()
{
BufferedReader inFromClient;
try{
inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
OutputStream outToClient = clientSocket.getOutputStream();
PrintWriter printOutPut = new PrintWriter(new OutputStreamWriter(outToClient),true);
String request= inFromClient.readLine();
if(request !=null)
{
if(!request.equalsIgnoreCase("finished"))
{
printOutPut.write("Receving data");
}
else
{
printOutPut.write("file received");
}
}
}catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
try
{
clientSocket.close();
}catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
client class
import java.io.*;
import java.net.*;
import java.util.concurrent.TimeUnit;
public class client{
public static void main(String[] args) throws Exception{
final int PORT=20020;
String serverHostname = new String("127.0.0.1");
Socket socket;
PrintWriter outToServer;
BufferedReader inFromServer;
BufferedReader inFromUser;
byte[] dataToTransfer;
String line;
int counter=0;
int i=0;
socket = new Socket(serverHostname, PORT);
outToServer = new PrintWriter(socket.getOutputStream(),true);
inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
inFromUser = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Simulation of file transferring");
System.out.println("Enter the file size you want to transfer (Max Size 50MB)");
int userInput = Integer.parseInt(inFromUser.readLine());
System.out.println("Transferring start");
boolean connection = true;
while(connection)
{
//set transfer rate at 1MB/s
dataToTransfer = new byte[1000000];
Thread.sleep(1000);
if(i<userInput)
{
outToServer.println(dataToTransfer);
counter++;
System.out.println(counter + "MB file has been transferred");
}
else
{
outToServer.println("Finished");
}
line = inFromServer.readLine();
System.out.println(line);
if(!line.equalsIgnoreCase("file received"))
{
}
else
{
System.out.println("Transfer completed");
break;
}
i++;
}
outToServer.close();
inFromServer.close();
inFromUser.close();
socket.close();
}
}
You are sending byte array from client to server and reading string on server side.
Insert somthing in your byte array and then Convert your byte array into String
String str = new String(dataToTransfer,int offset, 1000000);
then write:
outToServer.println(str);

Categories