Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,114 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,067 people online right now. Registration is fast and FREE... Join Now!




socket programming

 
Reply to this topicStart new topic

socket programming

kelvinlj38128
post 6 Oct, 2008 - 02:25 PM
Post #1


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 2

TCPClient.java
CODE

import java.io.*;
import java.net.*;
class TCPClient {
     public static void main (String argv[]) throws Exception
   {
      String sentence;
      String modifiedSentence;
      BufferedReader inFromUser = new BufferedReader( new InputStreamReader (System.in) );
      Socket clientSocket = new Socket ("hostname" , 6789);
      DataOutputStream outToServer = new DataOutputStream( clientSocket.getOutputStream() );
      BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
                   clientSocket.getInputStream() ) );
      sentence = inFromUser.readLine();
      outputToServer.writeBytes (sentence + '\n');
      modifiedSentence = inFromServer.readLine();
      System.out.println("FROM SERVER: " + modifiedSentence);
      clientSocket.close();
    }
}

TCPServer.java

import java.io.*;
import java.net.*;
class TCPServer {
  public static void main (String argv[] ) throws Exception
        {
             String clientSentence;
             String capitalizedSentence;
             ServerSocket welcomeSocket = new ServerSocket (6789);
             while (true)   {
                  Socket connectionSocket = welcomeSocket.accept();
                  BufferedReader inFromClient =
                       new BufferedReader (new InputStreamReader(
                            connectionSocket.getInputStream() ) );
                  DataOutputSteam( connectionSocket.getOutputStream() );
                  clientSentence = inFromClient.readLine();
                  captitalizedSentence = clientSentence.toUpperCase() + '\n';
                  outToClient.writeBytes (capitalizedSentence);
                }
          }
}

Question is which code if either of them do I have to modify and what modifications do I need to make in order to read and write on the console what is typed on the other side. You should also be able to see what you are typing. In this way you will have a console-base chat program.
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 6 Oct, 2008 - 02:31 PM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,837



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Moved to Java forum.
User is online!Profile CardPM

Go to the top of the page

pbl
post 6 Oct, 2008 - 04:15 PM
Post #3


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,946



Thanked 186 times

Dream Kudos: 75
My Contributions


Not that I want to preach for my writtings... but don't see why you would need a BufferedReader and all that stuff
Here is a simple one... you just have to add the System.out.println() to have your console application stuff

http://www.dreamincode.net/code/snippet1917.htm
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 10:10AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month