Results 1 to 3 of 3
  1. #1
    rosemary is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default To send and recieve data stored in fileusing TCP program

    Please help me....

    import java.io.*;
    import java.util.*;
    import java.net.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.lang.String.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class tcpserver extends JFrame implements ActionListener,Runnable

    {
    ServerSocket ss;
    Socket cs;
    PrintWriter pr;
    BufferedReader br;
    Thread t;
    JTextField t2;
    JButton b;
    Container c;
    String msg[][]=new String[10][10];
    int i=0,j;
    int bs;

    FileInputStream infile=null;
    tcpserver()

    {
    super("Server");
    c=getContentPane();
    c.setLayout(null);


    t2=new JTextField(100);

    b=new JButton("send");

    setSize(400,350);
    setLocation(30,30);
    setVisible(true);


    t2.setBounds(100,250,140,50);
    b.setBounds(125,150,80,50);


    c.add(t2);
    c.add(b);

    b.addActionListener(this);

    try
    {
    ss=new ServerSocket(2000);
    cs=ss.accept();
    br=new BufferedReader(new InputStreamReader(cs.getInputStream()));

    pr=new PrintWriter(cs.getOutputStream(),true);
    infile=new FileInputStream("input.txt");

    t=new Thread(this);
    t.start();

    }

    catch(Exception e)
    {
    System.out.println("sorry");
    }

    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent dr)
    {
    try
    {
    ss.close();
    cs.close();
    System.exit(0);
    }
    catch(Exception rgf)
    {}
    }
    });
    }
    public void actionPerformed(ActionEvent a)
    {
    if(a.getSource()==b)
    {try

    {
    while((bs=infile.read())!=-1)

    {
    pr.println((char)bs);
    }

    infile.close();
    }

    catch(Exception e)

    {}


    }
    }

    public void run()
    {
    for(;;)
    {
    try
    {


    if((br.readLine())!=null)
    {
    msg[0][i]=br.readLine();
    i++;
    }
    for(j=0;j<i;j++)
    {
    t2.setText(msg[0][j]);
    }


    }
    catch(Exception gf)
    {}
    }
    }
    public static void main(String args[])
    {
    tcpserver ch=new tcpserver();
    }
    }

    //client program
    import java.io.*;
    import java.util.*;
    import java.net.*;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class tcpclient extends JFrame implements ActionListener,Runnable
    {
    Socket cs;
    PrintWriter pr;
    BufferedReader br;
    Thread t;
    JTextField t2;
    JButton b;
    int ch,i=0,j;
    FileInputStream infile=null;
    Container c;

    String msg[][]=new String[50][];
    tcpclient()
    {
    super("client");
    c=getContentPane();
    c.setLayout(null);

    t2=new JTextField(100);
    b=new JButton("send");
    setSize(400,350);
    setLocation(300,300);
    setVisible(true);

    t2.setBounds(100,250,140,50);
    b.setBounds(125,150,80,50);
    c.add(t2);

    c.add(b);
    b.addActionListener(this);

    try
    {
    cs=new Socket(InetAddress.getLocalHost(),2000);
    br=new BufferedReader(new InputStreamReader(cs.getInputStream()));

    pr=new PrintWriter(cs.getOutputStream(),true);
    infile=new FileInputStream("out.txt");

    t=new Thread(this);
    t.start();
    t.join();

    }
    catch(Exception e) {
    System.out.println("sorry");
    }

    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent dr)
    {
    try
    {
    cs.close();
    System.exit(0);
    }
    catch(Exception rf)
    {}
    }
    });
    }
    public void actionPerformed(ActionEvent a)
    {
    if(a.getSource()==b)
    {
    try
    {
    while((ch=infile.read())!=-1)
    {
    pr.println((char)ch);

    }
    infile.close();
    }
    catch(Exception e)

    {}
    }


    }









    public void run()
    {
    for(;;)
    {
    try
    {
    if((br.readLine())!=null)
    {
    msg[0][i]=br.readLine();
    i++;
    }
    for(j=0;j<i;j++)
    {
    t2.setText(msg[0][j]);
    }






    }
    catch(Exception es)
    {}
    }
    }
    public static void main(String args[])
    {
    tcpclient ch=new tcpclient();
    }
    }


    please help me .....
    Last edited by rosemary; 01-31-2009 at 06:54 PM. Reason: for(;;) some icon is coming

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    What is the problem, and what help do you want from us?

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Start by looking at the tutorial.

    The core classes are ServerSocketChannel and SocketChannel.

Similar Threads

  1. How to insert a Value into a stored array ?
    By A.M.S in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 03:21 PM
  2. stored procedure
    By sankarigopi in forum JDBC
    Replies: 1
    Last Post: 11-13-2008, 05:53 PM
  3. Stored Procedures
    By geeta_ravikanti in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 03:34 AM
  4. Replies: 1
    Last Post: 01-28-2008, 11:49 AM
  5. Stored Procedures with java
    By Albert in forum JDBC
    Replies: 4
    Last Post: 06-08-2007, 06:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •