Click here to Skip to main content
15,913,055 members
Home / Discussions / Java
   

Java

 
QuestionProgressbar while some process is running Pin
002comp8-Jul-10 23:30
002comp8-Jul-10 23:30 
AnswerRe: Progressbar while some process is running Pin
David Skelly9-Jul-10 2:13
David Skelly9-Jul-10 2:13 
GeneralRe: Progressbar while some process is running Pin
002comp9-Jul-10 2:35
002comp9-Jul-10 2:35 
QuestionFile Object with URL? Pin
002comp5-Jul-10 23:37
002comp5-Jul-10 23:37 
AnswerRe: File Object with URL? Pin
Richard MacCutchan6-Jul-10 1:47
mveRichard MacCutchan6-Jul-10 1:47 
GeneralRe: File Object with URL? Pin
002comp6-Jul-10 19:57
002comp6-Jul-10 19:57 
GeneralRe: File Object with URL? Pin
Richard MacCutchan6-Jul-10 21:19
mveRichard MacCutchan6-Jul-10 21:19 
QuestionSingleton and TCP server Pin
Nadunwow2-Jul-10 7:07
Nadunwow2-Jul-10 7:07 
Guys

I'm trying to applying singleton pattern to TCP server client application

But I get some run time exceptions.I tryed out diffrent ways.
Please give me your suggetions to correct this

here TCPServer is the Singleton

in my client(TCPClient) I want to access TCPServer but I can not do it.

TCPServer Class

import java.io.*; 
import java.net.*; 

class TCPServer { 

	private String  capitalizedSentence; 
	private ServerSocket welcomeSocket;
	private String clientSentence = new String(" d");
	private BufferedReader inFromClient;
	private DataOutputStream  outToClient;
	private Socket connectionSocket;
	  
	private static TCPServer instance = null;
	  
	 private TCPServer() throws Exception {
	  
	  }
	  
	private  TCPServer(int socketID) throws Exception {
	  if( welcomeSocket==null){
	 
		 }
	}
	
	
	
	public static TCPServer getInstance() throws Exception {
      if(instance == null) {
         instance = new TCPServer();
		 System.out.println("This is new server instance created");
      }
	  else if(instance !=null){
	  System.out.println("This is old instance");
	  }
	  
      return instance;
   }
	
	public void method()
	{
		System.out.println("This is server");
	}
	public void run() throws Exception{
	 welcomeSocket = new ServerSocket(6789); 
		 while(clientSentence.equals("exit")!=true) { 
		  connectionSocket = welcomeSocket.accept(); 
		  inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
          outToClient = new DataOutputStream(connectionSocket.getOutputStream()); 
           clientSentence = inFromClient.readLine(); 
           System.out.println("Recived from client : " + clientSentence);  
           capitalizedSentence = clientSentence.toUpperCase() + '\n'; 
           outToClient.writeBytes(capitalizedSentence); 
        } 
	
	}


} 


TCP Client Class

import java.io.*; 
import java.net.*; 

class TCPClient { 

		private String sentence,modifiedSentence; 
		private DataOutputStream outToServer;
		private BufferedReader inFromServer;
		private Socket clientSocket;
		private BufferedReader inFromUser;
		
		public TCPClient() throws Exception{
		
		inFromUser =  new BufferedReader(new InputStreamReader(System.in)); 
        clientSocket = new Socket("192.168.1.2", 6789); 
        outToServer = new DataOutputStream(clientSocket.getOutputStream()); 
        inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
       
		}
		
		public String getSentence()
		{
		return sentence;
		}
		
		public void setSentence(String str)
		{
			this.sentence=str;
		}
		
		
		
		public String getModifiedSentence()
		{
			return modifiedSentence;
		}
		
		public void setModifiedSentence(String str )
		{
			this.modifiedSentence=str;
		}
		
		public void run() throws Exception{
		
		
		System.out.println("Enter a String");
		sentence = inFromUser.readLine(); 
		
        outToServer.writeBytes(sentence + '\n'); 
        modifiedSentence = inFromServer.readLine(); 
        System.out.println("FROM SERVER: " + modifiedSentence); 
		
		TCPServer mySever = TCPServer.getInstance();/*This is the place where I try to access server object*/
        clientSocket.close(); 
		
		}
      
 } 


Calling class

To Server

public class Server{
	public static void main(String[] args) throws Exception{
	  TCPServer x = TCPServer.getInstance();
	  x.run();
	}
}


To client

public class Client{
	public static void main(String[] args) throws Exception{
	TCPClient client = new TCPClient();
	client.run();
	}
}

AnswerRe: Singleton and TCP server Pin
Richard MacCutchan2-Jul-10 8:37
mveRichard MacCutchan2-Jul-10 8:37 
GeneralRe: Singleton and TCP server Pin
Nadunwow2-Jul-10 8:41
Nadunwow2-Jul-10 8:41 
GeneralRe: Singleton and TCP server Pin
Richard MacCutchan2-Jul-10 8:57
mveRichard MacCutchan2-Jul-10 8:57 
GeneralRe: Singleton and TCP server Pin
Nagy Vilmos6-Jul-10 3:53
professionalNagy Vilmos6-Jul-10 3:53 
GeneralRe: Singleton and TCP server Pin
Nagy Vilmos6-Jul-10 3:55
professionalNagy Vilmos6-Jul-10 3:55 
QuestionClass not found issue Pin
False Chicken30-Jun-10 19:54
False Chicken30-Jun-10 19:54 
AnswerRe: Class not found issue Pin
Cedric Moonen30-Jun-10 20:44
Cedric Moonen30-Jun-10 20:44 
GeneralRe: Class not found issue Pin
False Chicken30-Jun-10 20:50
False Chicken30-Jun-10 20:50 
QuestionHow can I use relative path in java to read a file ? Pin
002comp29-Jun-10 2:47
002comp29-Jun-10 2:47 
AnswerRe: How can I use relative path in java to read a file ? Pin
Nagy Vilmos29-Jun-10 11:47
professionalNagy Vilmos29-Jun-10 11:47 
GeneralRe: How can I use relative path in java to read a file ? Pin
002comp29-Jun-10 18:05
002comp29-Jun-10 18:05 
GeneralRe: How can I use relative path in java to read a file ? Pin
002comp29-Jun-10 19:05
002comp29-Jun-10 19:05 
GeneralRe: How can I use relative path in java to read a file ? Pin
002comp29-Jun-10 21:26
002comp29-Jun-10 21:26 
GeneralRe: How can I use relative path in java to read a file ? Pin
Richard MacCutchan29-Jun-10 22:03
mveRichard MacCutchan29-Jun-10 22:03 
GeneralRe: How can I use relative path in java to read a file ? Pin
002comp29-Jun-10 22:32
002comp29-Jun-10 22:32 
GeneralRe: How can I use relative path in java to read a file ? Pin
002comp30-Jun-10 0:11
002comp30-Jun-10 0:11 
GeneralRe: How can I use relative path in java to read a file ? Pin
Richard MacCutchan30-Jun-10 1:28
mveRichard MacCutchan30-Jun-10 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.