Solved

server /client Java code (simple form...)

Posted on 2007-11-25
4
1,891 Views
Last Modified: 2013-11-23
I want to fill properly the following code to connect with a server and to bet back (echo) anything I type...
import java.net.*;
import java.io.*;
import java.util.*;
class ServerTest{
	public static void main(String[] args)throws UnknownHostException,IOException{
	
	Scanner input=new Scanner(System.in);	
	Socket client;
	String serverName="pop.mail.yahoo.com";
	try{
	//trying to establish a connection to serverName
	client=new Socket(serverName,110);
	System.out.println("connected with "+serverName);
	System.out.print("enter a string to be echoed: ");
	String string=input.nextLine();
	//what I have to write here??????
	}
	catch(UnknownHostException e){
		System.out.println("exception: "+e);
	}
	catch (IOException e){
		System.out.println("exception : "+e);
	}	
	}
}

Open in new window

0
Comment
Question by:dimi67
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20347153
0
 

Author Comment

by:dimi67
ID: 20348866
well, this is not what I 'm asking...
0
 
LVL 17

Accepted Solution

by:
contactkarthi earned 500 total points
ID: 20365888
have a look at the question in the following post

http://www.experts-exchange.com/Programming/Languages/Java/Q_22959757.html

the asker has a program that send info to server and he gets responses back from server

below are some lines from there that you are looking

 String remoteServer = "localhost";
         
        try {
            // Try connecting to the 'remote' machine on port 4445
            serverSocket = new Socket(remoteServer, 4445);
            toServer = new PrintWriter(serverSocket.getOutputStream(), true);
            fromServer = new BufferedReader(new InputStreamReader(
                                        serverSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + remoteServer);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection to: " + remoteServer);
            System.exit(1);
        } // try-catch


toServer.println(balance); // send message to server
reply = fromServer.readLine();// get response from server
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20365946
or look in

http://www.exampledepot.com/egs/java.net/CreateServerSocket.html

and its related links at the bottom of the above page
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The article explains the protocols and technology which is involved when two computers on different TCP/IP networks communicate with each other. In the diagram, a router is used to segregate two networks. The networks are 192.168.1.0/24 and 192…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question