Solved

socket programming in java

Posted on 2004-09-29
17
285 Views
Last Modified: 2010-03-31
i am doing socket programming. i am able to send data from client to server using one computer. however, i could not send data to other computer using same java code. another problem is only client can send data to server. how can i enable server also can send data to client? pls help.
i enclosed my java code below:

// tcpClient.java by fpont 3/2000

// usage : java tcpClient <server> <port>
// default port is 1500

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

public class tcpClient {



public static void main(String[] args) {

int port = 1500;
String server = "localhost";
Socket socket = null;
String lineToBeSent;
BufferedReader input;
PrintWriter output;
int ERROR = 1;

// read arguments
if(args.length == 2) {
server = args[0];
try {
port = Integer.parseInt(args[1]);
}
catch (Exception e) {
System.out.println("server port = 1500 (default)");
port = 1500;
}
}



// connect to server
try {
socket = new Socket(server, port);
System.out.println("Connected with server " +
socket.getInetAddress() +
":" + socket.getPort());
}
catch (UnknownHostException e) {
System.out.println(e);
System.exit(ERROR);
}
catch (IOException e) {
System.out.println(e);
System.exit(ERROR);
}



try {
input = new BufferedReader(new InputStreamReader(System.in));
output = new PrintWriter(socket.getOutputStream(),true);

// get user input and transmit it to server
while(true) {
lineToBeSent = input.readLine();
// stop if input line is "."
if(lineToBeSent.equals(".")) break;
output.println(lineToBeSent);
}
}
catch (IOException e) {
System.out.println(e);
}

try {
socket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}

-------------------------------------------------------------------------------------------------------------------------------------------------
// tcpServer.java by fpont 3/2000

// usage : java tcpServer <port number>.
// default port is 1500.
// connection to be closed by client.
// this server handles only 1 connection.

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

public class tcpServer {

public static void main(String args[]) {

int port;
ServerSocket server_socket;
BufferedReader input;

try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
System.out.println("port = 1500 (default)");
port = 1500;
}

try {

server_socket = new ServerSocket(port);
System.out.println("Server waiting for client on port " +
server_socket.getLocalPort());

// server infinite loop
while(true) {
Socket socket = server_socket.accept();
System.out.println("New connection accepted " +
socket.getInetAddress() +
":" + socket.getPort());
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// print received data
try {
while(true) {
String message = input.readLine();
if (message==null) break;
System.out.println(message);
}
}
catch (IOException e) {
System.out.println(e);
}

// connection closed by client
try {
socket.close();
System.out.println("Connection closed by client");
}
catch (IOException e) {
System.out.println(e);
}

}


}

catch (IOException e) {
System.out.println(e);
}
}
}
0
Comment
Question by:skychan
  • 4
  • 4
  • 3
  • +1
17 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 43 total points
ID: 12177733
You need to get the server to open a socket to a ServerSocket on the client
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 41 total points
ID: 12177734
There should be no problem, if it works locally and not remotely then I'd say there is no problem with the Java code. Can you ping the other computer you are trying to access? Do you get any exceptions/error messages?
0
 

Author Comment

by:skychan
ID: 12177761
I want to send data by client (example:string )and server takes this string and wiev data
but i can't do it ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12177773
a.
>>
I want to send data by client (example:string )and server takes this string and wiev data
but i can't do it ?
>>

b.
>>
how can i enable server also can send data to client? pls help.
>>

Please be clear - which of the above do you want to do - a., b. or both?
0
 

Author Comment

by:skychan
ID: 12177806
hi cehj
both a an b
i  should do both...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12177821
Then you should have a ServerSocket both ends. Do you?
0
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 41 total points
ID: 12177838
Why do you not use the RemoteMethodInvocation.
in this manner you send something (calling a method) to the server. It create something and can answer to the request.

Bye, Giant.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

by:girionis
ID: 12177839
> i am able to send data from client to server using one computer.

As the above comment of your suggest you have already done it. The problem is in the network between the two computers. As I see from code you psoted you try to conenct to localhost. This is not the case anymore asince the server rusn on a different computer. You need to chaneg this line:

String server = "localhost";

with the IP or DNS of the computer you are trying to connect to.
0
 

Author Comment

by:skychan
ID: 12177871
hi Giant2
i don' t known anything RemoteMethodInvocation.
If you can write  one or more example I can use RemoteMethodInvocation.
 
0
 

Author Comment

by:skychan
ID: 12177889
Cehj
i don't have ServerSocket both .
ok i send only client to server and server wiev data
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12178025
>>ok i send only client to server and server wiev data

You should decide on what your program should do before you start wasting your points by asking questions about it
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12178071
RemoteMethodInvocation is RMI.
As CEHJ suggest you, "should decide on what your program should do before you start wasting your points by asking questions about it".
See the system actually available doing this you want (for RMI see http://java.sun.com/docs/books/tutorial/rmi/).

So, wait a moment. See better what you want and see if there are environment/api already doing it.

Bye, Giant.
0
 
LVL 35

Expert Comment

by:girionis
ID: 12399765
My comments offer a possible solution. I'd say I am worth some points.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12408078
I think a split could be done.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 78
triangle challenge 4 79
allStar challenge 1 43
backtracking recursion  code 19 41
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now