How to check my server socket programs are correct...?

hello friend's
Just started to read about socket programming.I have written a simple program for reading a file from server and write in client side.
Check this,
For server side....
import java.io.*;
import java.net.*;
public class ss
{
    public static void main(String args[])
    {
        byte b[]=new byte[60];
        try
        {
            ServerSocket s=new ServerSocket(12345);
            Socket s1=s.accept();
            File f=new File("kk.txt");
            BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f));
            bis.read(b);
            OutputStream os=s1.getOutputStream();
            os.write(b);
            s1.close();
        }
        catch(Exception e)
        {
            System.out.println("Exception caught ");
        }
    }
}        
For client side....
import java.io.*;
import java.net.*;
public class ss
{
    public static void main(String args[])
    {
        byte b[]=new byte[60];
        try
        {
            Socket s=new Socket("27.0.0.1",1234);
            InputStream is=s.getInputStream();
            is.read(b);
            FileOutputStream f=new FileOutputStream("monu.txt");
            BufferedOutputStream bos=new BufferedOutputStream(f);
            bos.write(b);
            bos.close();
            s.close();
        }
        catch(Exception e)
        {
            System.out.println("Exception caught ");
        }
    }
} 
But the problem is that i want to check that my program working fine or not but i don't know how can i check this.
Can i use any hosting account as a server ?
If yes then what to do in order to execute this program ?

Replies

  • Manish Goyal
    Manish Goyal
    no need of server

    Just open command prompt twice and execute both programs separately
  • Morningdot Hablu
    Morningdot Hablu
    @goyal are there any modification needed in my program ?
    is it working fine with your pc ?
    I mean i just edited my ip address from the client program isn't there any modification issue related with port number ?
  • Morningdot Hablu
    Morningdot Hablu
    I have done it...it works fine in my own pc.I just have to specify 127.0.0.1 in the place of my address but while i give my own ip it will caught an exception.
    Can you tell me why..?
    .
    One more thing it will also caught an exception when i specify my friends ip(here i use my own pc as a client and my friend pc as a server both are connected through internet).
  • dipen30
    dipen30
    That's because you trying to open different port on client side and server side.
    You have to open same port on client side which you gave to server.

    In your program at server side you open port "12345" and client side "1234".
    keep port "12345" at client side and then try it.
  • Morningdot Hablu
    Morningdot Hablu
    By mistake i posted wrong port for the second one.
    Actually i am opening the same port.And i will tested my firewall also it will allow to do the connection.After adding printStackTrace() it will show an exception like "connection timed out".I don't think it's an issue with my low connectivity.But don't know why i doesn't works.
  • Sachin Jain
    Sachin Jain
    @ Mohit
    Change your port no again and then try.
    I think it will work then..

You are reading an archived discussion.

Related Posts

Dear friends! Harmonics in electricity is always undesirable.So please can u suggest me some methods to reduce harmonics in power supply and how are they harmfull? PLease help me out
Dear friends! Sorry! But i dont have much knowledge of CMOS LOgic circuits? can you please explain me what are they and how to use them? thanks in advance.
Dear friends please explain me: total harmonic distortion Displacement factor Input power factor Input power factor is power factor only i suppose..Please tell me if i am right. Other terms...
[FONT="]Boiler (Steam Generator)[/FONT] -The boiler for 1,094 t/h MCR is Single Drum Radiant Natural Circulation Boiler “ELPASO” Type which has self-drainable heating surface. The unit is of pressurized furnace design...
Hello guys, I hope you are all well! I am creating a game in Visual Studio using picture box's, but I cannot for the life of me figure out how...