Hi, I am trying to create a Client/Server application...when client is initialized it reads a file line by line and sends each line to the server which does some sort of action to the line (in my code it capitalizes the line) and writes it to a file. I have the client/server connection working fine and the reading is working fine. Currently when I run the client it sends all lines to the server, one by one, and the server prints each one to the screen capitalized...now the problem is when writing those lines (instead of printing them) to a file. I have read up on how to make it write to a file in Java and it works but its only printing the first or last line no matter how I shuffle the code around, any ideas how to fix it?
import java.io.*;
import java.net.*;
class UDP_Server {
public static void main(String args[]) throws Exception
{
//create DatagramSocket
DatagramSocket serverSocket = new DatagramSocket(9876);
//create byte array to contain msgs received from client and to send to client
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
FileWriter fwriter = new FileWriter("file.txt");
BufferedWriter writer = new BufferedWriter(fwriter);
while(true) {
//create and receive packet from client
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
//get data from packet received from client
String sentence = new String(receivePacket.getData());
//get IP address and port number
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
//capitalize the packet msg received from client
String capitalizedSentence = sentence.toUpperCase();
writer.write(capitalizedSentence);
writer.newLine();
writer.close();
//send the new capitalized msg into a packet to the client
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}
I think it might have to do with the position of the close() but even if I changed it around I couldn't get it to work. Thanks for any insight and sorry if its a newbie question :)
EDIT: that is just the server code and its using UDP.