![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 17
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|