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

    Updated: Oct 26, 2024
    Views: 1.1K
    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 ?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Manish Goyal

    MemberJan 2, 2011

    no need of server

    Just open command prompt twice and execute both programs separately
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberJan 2, 2011

    @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 ?
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberJan 2, 2011

    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).
    Are you sure? This action cannot be undone.
    Cancel
  • dipen30

    MemberJan 2, 2011

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberJan 2, 2011

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Sachin Jain

    MemberJan 2, 2011

    @ Mohit
    Change your port no again and then try.
    I think it will work then..
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register