Click to See Complete Forum and Search --> : Client Server Sockets~~ Help Please


PinkPrincess
March 13th, 2005, 03:56 AM
Hi Oll...

the assignment was to make client say hello when the connection is established and then the server will say Who are you ? then the client will say Am client # Show the ip address# and then the server will say Ok the host is reachable

but we should not use chat way the scenario must be shown directly and written in the code so i tried but it's not working .. i just start by HEllo and Who are you

i couldn't complete can any one help me


The Sock_serv.java file :

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

public class Sock_serv {
public static void main(String[] args) {
ServerSocket server_sock = null;
Socket server_connection = null;

try {

server_sock = new ServerSocket(5004);

server_connection = server_sock.accept();

if (server_connection != null) {
try {
OutputStream Output_to_Client;
String s;

Output_to_Client = server_connection.getOutputStream();
s = new String ("WHO ARE YOU?\n");
for (int j = 0; j < s.length(); j++){
Output_to_Client.write ((int) s.charAt (j));}


server_connection.close() ;
}catch (IOException e) {
System.err.println("I/O error on connection to Client");
}
} /* end if */
}

catch (IOException e) {
System.err.println ("I/O error on Server Socket");
}
} /* end main() */
} /* end class */




the Client.java File :

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

public class Client {

public static void main(String[] args) {
Socket a_connection = null;


try {

a_connection = new Socket("127.0.0.1", 5004);
}
catch (UnknownHostException e)
{
System.err.println("Host unreachable");
}
catch (IOException e)
{
System.err.println("I/O error for the connection to Yoda");
}

if (a_connection != null) {
try {



String M = new String ("Hello!\n");

System.out.println("Client: " + M);



String c;
InputStream Input_from_Server;

Input_from_Server = a_connection.getInputStream();

while ((c = (char) Input_from_Server.read()) !='\n')
System.out.print ("Server : " + c);
System.out.println();
}
a_connection.close();
}
catch (IOException e)
{
System.err.println("I/O error on connection to Yoda");
}
}
}
}

cma
March 13th, 2005, 03:16 PM
First and foremost, when posting code, surround them with so-called code-tags in your post.


....code....


this will make it easier for everyone to interpret your code at a glance.

Now, you have to write your code using lower level read() and write() operations and you can't use BufferedReader and PrintWriter (if this is what you mean by "chat way")? If so, then you can still simplify the process a bit, in your server code, you can write out the entire string with one method:

OutputStream out = server_connection.getOutputStream();
String s = "WHO ARE YOU?\n";
out.write(s.getBytes());


From the client side, you can either read a single byte at a time and append it to a StringBuffer or you can read several bytes at a time and append the results to a String, whichever seems easier to you:

InputStream in = a_connection.getInputStream();
StringBuffer sb = new StringBuffer();
int data = 0;

while ((data = in.read()) > 0)
sb.append((char) data);

System.out.println(sb);

cjard
March 14th, 2005, 08:47 PM
hi.. just a few notes on code conventions we have in java. if you could use them (i dont normally advise this if theres a coding standard in lace, but your code is inconsistent in its naming so im guesing the stadard is not in place) then it would help people review your code

variableNamesLookLikeThis
methodNamesLookLikeThis
ClassNamesLookLikeThis
FINAL_VARIABLES_LOOK_LIKE_THIS


note the lowercase first letter for methods and variables, upper case for strings:

example:

public void mainMethodThing(String myString, final int MAX_COUNT) as String..

PinkPrincess
March 14th, 2005, 11:19 PM
Thanks to Oll

well am a bad java programmer ... my java skills are so weak .. and i can't understand alot coz my basics are weak.. so sorry if my code was no good :)