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");
}
}
}
}
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");
}
}
}
}