Sending information over tcp/ip or internet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Sending information over tcp/ip or internet

  1. #1
    Join Date
    Jul 2004
    Posts
    83

    Sending information over tcp/ip or internet

    Hey, I have a client program that I would like to send a String or any type of information to the server, I was wondering how you would send the information to the server as well as have the server read it, thanks!
    They say if you play a Microsoft Windows CD backwards it will play satanic messages. But thats nothing, if you play it forwards it installs Windows.

  2. #2
    Join Date
    Sep 2005
    Location
    TX
    Posts
    23
    Do have some code that you've been working on??

    If not, take at look here for starters: http://java.sun.com/docs/books/tutor...ets/index.html

  3. #3
    Join Date
    Oct 2005
    Posts
    107

  4. #4
    Join Date
    Nov 2005
    Posts
    9
    Hello,
    Here it is a simple client/server application:

    SERVER:

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

    class reply{

    ServerSocket server=null; //soclul server-ului
    Socket ClientSocket=null; //soclul returnat dupa aceptarea conexiunii
    PrintWriter out=null; //fluxul de iesire
    BufferedReader in=null; //fluxul de intrare
    InputStreamReader ISR=null;
    String intrare=null;
    String iesire=null;

    public reply(){}

    //realizarea conexiunii
    protected void connect()
    {
    try{
    server=new ServerSocket(2008); //port-ul server-ului va fi 2008
    }catch(IOException e)
    {
    System.out.println("Eroare 1:"+e.getMessage());
    System.exit(1);
    }

    try{
    //se asteapta conectarea clientilor
    System.out.println("Astept clientii!!!");
    ClientSocket=server.accept();
    }catch(IOException e)
    {
    System.out.println("Eroare 2:"+e.getMessage());
    System.exit(1);
    }

    try{
    //dupa realizarea conexiunii se pregatesc fluxurile I/O ce vor facilita transferul de informatii
    System.out.println("Client conectat.");
    out=new PrintWriter(ClientSocket.getOutputStream(),true);
    ISR=new InputStreamReader(ClientSocket.getInputStream());
    in=new BufferedReader(ISR);

    //primul mesaj de la server pe care il vede client-ul
    out.println("Buna, tocmai te-ai conectat!!!");

    //atata timp cat client-ul trimite text diferit de "Pa!" server-ul il trimite inapoi
    //pe post de raspuns
    while((intrare=in.readLine())!=null)
    {
    System.out.println("Recive:"+intrare);
    if(intrare.equals("Pa!"))break;
    out.println("reply: "+intrare);
    }

    //dupa "ruperea" conexiunii server-ul "face curat"
    System.out.println("Conexiune anulata!!!");
    out.close();
    in.close();
    ClientSocket.close();
    server.close();

    }catch(IOException e)
    {
    System.out.println("Eroare 3:"+e.getMessage());
    System.exit(1);
    }
    }
    }

    public class ServerSide{
    public static void main(String[] args)
    {
    reply t=new reply();
    t.connect();
    }
    }

    CLIENT:

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

    class contact_server{

    Socket soclu=null; //soclul client-ului
    PrintWriter out=null; //flux de iesire
    BufferedReader in=null; //flux de intrare
    InputStreamReader ISR=null;
    String intrare=null;
    String iesire=null;

    BufferedReader message=null; //s-a utilizat un obiect BufferedReader pentru a permite introducerea
    //de text de la consola

    public contact_server(){}

    void send_message()
    {
    try{
    //pentru cei care ruleaza aplicatiile pe masini diferite
    //va puteti conecta folosind adresa IP a server-ului
    //InetAddress addr = InetAddress.getByName("10.44.65.81");
    //daca nu aveti un server si rulati ambele aplicatii pe acelasi calculator
    //atunci procedati astfel:
    InetAddress addr = InetAddress.getLocalHost();

    int port = 2008;

    //se creeaza soclul folosind adresa si port-ul pe care ruleaza server-ul
    soclu=new Socket(addr,port);
    }catch(IOException e)
    {
    System.out.println("Eroare 1:"+e.getMessage());
    System.exit(1);
    }

    //se deschid fluxurile I/O pentru a putea comunica cu server-ul
    try{
    out=new PrintWriter(soclu.getOutputStream(),true);
    ISR=new InputStreamReader(soclu.getInputStream());
    in=new BufferedReader(ISR);

    //trimiterea de mesaje server-ului se poate face atata timp cat acesta
    //nu returneaza un null
    while((intrare=in.readLine())!=null)
    {
    //se tipareste la consola mesajul receptionat de la server
    System.out.println("Server "+intrare);

    //se permite introducerea de text de la consola
    message=new BufferedReader(new InputStreamReader(System.in));
    System.out.flush();
    System.out.print("Send:");
    iesire=message.readLine();
    //se trimite efectiv mesajul dorit catre server
    if (iesire!=null)out.println(iesire);
    else break;
    }

    //se inchide conexiunea
    System.out.println("Conexiune anulata!!!");
    out.close();
    in.close();
    message.close();
    soclu.close();

    }catch(IOException e)
    {
    System.out.println("Eroare 2:"+e.getMessage());
    System.exit(1);
    }
    }
    }

    public class ClientSide{
    public static void main(String[] args){
    contact_server t=new contact_server();
    t.send_message();
    }
    }

    P.S. Ignore the code // !!!

Similar Threads

  1. ANN: Catalyst Internet Mail V4.0
    By Catalyst Development Corporation in forum vb.announcements
    Replies: 0
    Last Post: 12-13-2002, 09:11 AM
  2. Information required for TCP/IP Socket Error
    By Ashish Patankar in forum ASP.NET
    Replies: 3
    Last Post: 04-15-2002, 08:58 AM
  3. Replies: 2
    Last Post: 07-03-2001, 04:46 PM
  4. Replies: 2
    Last Post: 01-15-2001, 05:09 PM
  5. Trouble finding api information on Internet functions
    By Jonathan Martindell in forum authorevents.appleman
    Replies: 1
    Last Post: 04-12-2000, 01:37 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center