Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 23rd, 2005, 12:06 AM   #1
GoO
Newbie
 
Join Date: Nov 2004
Posts: 17
Rep Power: 0 GoO is on a distinguished road
Client/server programming question...

how do you make the client and the server exchange information? do you have to make a ServerSocket at the client too? or just both an InputStream and OutputStream?

Server code: (ServerProg.java)
// START THE SERVER
public void startServer() {
	String hostMessage = "";
	int checker = 0;
	// Register your services on port 4500
	assignSocket:
	while (checker == 0) {
		try {
			sServer = new ServerSocket(mySocket);
		} catch (IOException e) {
			//e.printStackTrace();
			mySocket++;
			continue assignSocket;
		}
		checker = 1;
		ta1.append("Server Started at DATE TIME\n");
		ta1.append("Server IP: 127.0.0.1\n");
		ta1.append("Server Socket: " + mySocket + "\n");
	}
	
	// Run the listen/accept loop forever
	while (sState == "running") {
		try {
			// Wait here and listen for a connection
			Socket s1 = sServer.accept();
			
			BufferedReader br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
			
			hostMessage = br.readLine();
			ta1.append("User at " + s1.getInetAddress() + " connected as \"" + hostMessage + "\"\n");
			
			// Close the connection, but not the server socket
			br.close();
			s1.close();				
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Client code: (ClientProg.java)
// Connect to the Server
public String connectServer(String arg1, String arg2, int arg3, String arg4) {
	String myMsg = "";
	try {
		// Open your connection to a server, at port 5432
		// localhost used here
		Socket s1 = new Socket(arg2, arg3);
		
		// Connect a reader to the socket
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));
				
		// send message
		myMsg = arg4;
		bw.write(myMsg);
			
		// When done, just close the stream and connection
		bw.close();
		s1.close();			
	} catch (ConnectException connEx) {
		return "Could not connect to the server.";
	} catch (IOException e) {
		// ignore
	}
		return "Success";
}

please tell mo where/how to make them exchange information. all i can do is the client or server sends a message and the other receives it. how do i make the client send a message to the server, then wait for a response from the server?
GoO is offline   Reply With Quote
Old Aug 26th, 2005, 6:17 AM   #2
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
what i do, is create athread that will sit and wait for the client to send a message. this thread must sit and wait with a readline() or something. as soon as the client sends something, the readline reads that message, then the thread will do whatever with it and then send a message back to the client and go wait at readline() again. your readline() you use with your inputstream object.
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Old Aug 26th, 2005, 8:52 AM   #3
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
Forgive me if I'm being dense, but your code examples aren't the complete files, are they? Everything in Java is a class; your ClientProg.java and ServerProg.java files only seem to contain methods which are not in any particular class.

Anyway, assuming both your programs compile and what you want is for them to talk to each other, you only need a ServerSocket in the server, to listen for and accept connections. The client uses a Socket, but not a ServerSocket. The ServerSocket is only required if you want the program to receive connections; Socket is fine if you just want to initiate them.

Hope this helps.
mackenga is offline   Reply With Quote
Old Aug 27th, 2005, 5:18 AM   #4
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
oh, yes. that too. ServerSocket waits for someone to connect. Socket connects to the ServerSocket, and the ServerSocket then transfers the connection to another port and then continues to listen for connections on port 10000 or whatever port you wanted to use.

it's pretty much the same as dialing into an internet server:

you dial into the server, it creates a connection between you and itself, transfers you to another line and then goes back to waiting for people to dial in again on the original number... or something like that.
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:48 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC