|
-
Threads, Accessing shared resource
Hi,
I've written a client-server program, the client inputs data, and the server accepts that data and writes it into a text file(file gets created when program is run). Works fine for a single client, now if I have 2 instances of the client running, i want the server to write the data input by client1 and client2 into the text file, what happens in my program is, client1's data is overwritten by client2's data. please can you help me correct this error.
Thanks,
Rohan.
-------------------------
SocketClient file:
import java.net.*;
import java.io.*;
public class SocketClient {
public static void main(String[] args) {
String host = "localhost";
int port = 19999;
System.out.println("SocketClient initialized");
try {
InetAddress address = InetAddress.getByName(host);
Socket connection = new Socket(address, port);
try {
while(true){
PrintWriter output = new PrintWriter(connection.getOutputStream(),true);
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Query: ");
String lineToBeSent = input.readLine();
output.println(lineToBeSent);
}
}
catch (IOException e) {
System.out.println(e);
}
try{
connection.close();
}catch(IOException e){
System.out.println(e);
}
}
catch (IOException f) {
System.out.println("IOException: " + f);
}
}
}
---------------------------------------------------------
MultipleSocketServer file:
import java.net.*;
import java.io.*;
import java.util.*;
public class MultipleSocketServer implements Runnable {
private Socket connection;
private String TimeStamp;
private int ID;
public static void main(String[] args) {
int port = 19999;
int count = 0;
try{
ServerSocket socket1 = new ServerSocket(port);
System.out.println("MultipleSocketServer Initialized");
while (true) {
Socket connection = socket1.accept();
Runnable runnable = new MultipleSocketServer(connection, ++count);
Thread thread = new Thread(runnable);
thread.start();
}
}
catch (Exception e) {}
}
MultipleSocketServer(Socket s, int i) {
this.connection = s;
this.ID = i;
}
public void run() {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintStream out1 =new PrintStream(new FileOutputStream("test.txt"));
while(true) {
String message = input.readLine();
out1.println(message);
out1.println();
System.out.println("Record Added to file");
}
}
catch (IOException e) {
System.out.println(e);
}
finally {
try {
connection.close();
}
catch (IOException e){}
}
}
}
Similar Threads
-
By Rob Teixeira in forum .NET
Replies: 129
Last Post: 06-06-2002, 05:23 AM
-
By Rob Teixeira in forum .NET
Replies: 15
Last Post: 05-31-2002, 03:30 PM
-
By Simon Fisher in forum ASP.NET
Replies: 1
Last Post: 01-23-2002, 09:10 AM
-
By Patrick Ireland in forum .NET
Replies: 5
Last Post: 05-10-2001, 06:19 PM
-
By Patrick Ireland in forum .NET
Replies: 0
Last Post: 04-26-2001, 10:01 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks