| | |
Using Server's in java.
![]() |
•
•
Join Date: May 2009
Posts: 10
Reputation:
Solved Threads: 0
Hi, i created a program but i need some help. I made it so the user must enter the ip address and then click the button CONNECT. But in java it outputs the same thing. Is there any way i can get rid of it and just start the program with the connect button only. I will post the code when i go to school tommorow. I will grab it off my computer at school.
Hi Joker21,
If you want to talk to us then you approach the daniweb, same way your java program need a valid and available IP-Address. Yes, It is also possible that you might have no permission or a firewall reject the connection request.
If you want to talk to us then you approach the daniweb, same way your java program need a valid and available IP-Address. Yes, It is also possible that you might have no permission or a firewall reject the connection request.
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: May 2009
Posts: 10
Reputation:
Solved Threads: 0
//The client code Client.java:
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class TestClient {
String ip;
ObjectInputStream Sinput; // to read the socker
ObjectOutputStream Soutput; // towrite on the socket
Socket socket;
// Constructor connection receiving a socket number
TestClient(int port) {
// we use "localhost" as host name, the server is on the same machine
// but you can put the "real" server name or IP address
try {
socket = new Socket("localhost", port);
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());
/* Creating both Data Stream */
try
{
Sinput = new ObjectInputStream(socket.getInputStream());
Soutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
// now that I have my connection
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the ip address to connect to:");
ip = scan.next();
// send the string to the server
System.out.println("Client sending \"" + ip + "\" to serveur");
try {
Soutput.writeObject(ip);
Soutput.flush();
}
catch(IOException e) {
System.out.println("Error writting to the socket: " + e);
return;
}
// read back the answer from the server
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server: " + response);
}
catch(Exception e) {
System.out.println("Problem reading back from server: " + e);
}
try{
Sinput.close();
Soutput.close();
}
catch(Exception e) {}
}
public static void main(String[] arg) {
new TestClient(1500);
}
}
//The server code Server.java:
import java.io.*;
import java.net.*;
/**
* This is to help people to write Client server application
* I tried to make it as simple as possible... the client connect to the server
* the client send a String to the server the server returns it in UPPERCASE thats all
*/
public class TestServer {
// the socket used by the server
private ServerSocket serverSocket;
// server constructor
TestServer(int port) {
/* create socket server and wait for connection requests */
try
{
serverSocket = new ServerSocket(port);
System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
while(true)
{
Socket socket = serverSocket.accept(); // accept connection
System.out.println("New client asked for a connection");
TcpThread t = new TcpThread(socket); // make a thread of it
System.out.println("Starting a thread for a new Client");
t.start();
}
}
catch (IOException e) {
System.out.println("Exception on new ServerSocket: " + e);
}
}
// you must "run" server to have the server run as a console application
public static void main(String[] arg) {
// start server on port 1500
new TestServer(1500);
}
/** One instance of this thread will run for each client */
class TcpThread extends Thread {
// the socket where to listen/talk
Socket socket;
ObjectInputStream Sinput;
ObjectOutputStream Soutput;
TcpThread(Socket socket) {
this.socket = socket;
}
public void run() {
/* Creating both Data Stream */
System.out.println("Thread trying to create Object Input/Output Streams");
try
{
// create output first
Soutput = new ObjectOutputStream(socket.getOutputStream());
Soutput.flush();
Sinput = new ObjectInputStream(socket.getInputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
System.out.println("Thread waiting for a String from the Client");
// read a String (which is an object)
try {
String str = (String) Sinput.readObject();
str = str.toUpperCase();
Soutput.writeObject(str);
Soutput.flush();
}
catch (IOException e) {
System.out.println("Exception reading/writing Streams: " + e);
return;
}
// will surely not happen with a String
catch (ClassNotFoundException o) {
}
finally {
try {
Soutput.close();
Sinput.close();
}
catch (Exception e) {
}
}
}
}
}
I am using this code from another website but can u teach me how to make it so that the user on the computer who received the message can send a message back?
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class TestClient {
String ip;
ObjectInputStream Sinput; // to read the socker
ObjectOutputStream Soutput; // towrite on the socket
Socket socket;
// Constructor connection receiving a socket number
TestClient(int port) {
// we use "localhost" as host name, the server is on the same machine
// but you can put the "real" server name or IP address
try {
socket = new Socket("localhost", port);
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());
/* Creating both Data Stream */
try
{
Sinput = new ObjectInputStream(socket.getInputStream());
Soutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
// now that I have my connection
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the ip address to connect to:");
ip = scan.next();
// send the string to the server
System.out.println("Client sending \"" + ip + "\" to serveur");
try {
Soutput.writeObject(ip);
Soutput.flush();
}
catch(IOException e) {
System.out.println("Error writting to the socket: " + e);
return;
}
// read back the answer from the server
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server: " + response);
}
catch(Exception e) {
System.out.println("Problem reading back from server: " + e);
}
try{
Sinput.close();
Soutput.close();
}
catch(Exception e) {}
}
public static void main(String[] arg) {
new TestClient(1500);
}
}
//The server code Server.java:
import java.io.*;
import java.net.*;
/**
* This is to help people to write Client server application
* I tried to make it as simple as possible... the client connect to the server
* the client send a String to the server the server returns it in UPPERCASE thats all
*/
public class TestServer {
// the socket used by the server
private ServerSocket serverSocket;
// server constructor
TestServer(int port) {
/* create socket server and wait for connection requests */
try
{
serverSocket = new ServerSocket(port);
System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
while(true)
{
Socket socket = serverSocket.accept(); // accept connection
System.out.println("New client asked for a connection");
TcpThread t = new TcpThread(socket); // make a thread of it
System.out.println("Starting a thread for a new Client");
t.start();
}
}
catch (IOException e) {
System.out.println("Exception on new ServerSocket: " + e);
}
}
// you must "run" server to have the server run as a console application
public static void main(String[] arg) {
// start server on port 1500
new TestServer(1500);
}
/** One instance of this thread will run for each client */
class TcpThread extends Thread {
// the socket where to listen/talk
Socket socket;
ObjectInputStream Sinput;
ObjectOutputStream Soutput;
TcpThread(Socket socket) {
this.socket = socket;
}
public void run() {
/* Creating both Data Stream */
System.out.println("Thread trying to create Object Input/Output Streams");
try
{
// create output first
Soutput = new ObjectOutputStream(socket.getOutputStream());
Soutput.flush();
Sinput = new ObjectInputStream(socket.getInputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
System.out.println("Thread waiting for a String from the Client");
// read a String (which is an object)
try {
String str = (String) Sinput.readObject();
str = str.toUpperCase();
Soutput.writeObject(str);
Soutput.flush();
}
catch (IOException e) {
System.out.println("Exception reading/writing Streams: " + e);
return;
}
// will surely not happen with a String
catch (ClassNotFoundException o) {
}
finally {
try {
Soutput.close();
Sinput.close();
}
catch (Exception e) {
}
}
}
}
}
I am using this code from another website but can u teach me how to make it so that the user on the computer who received the message can send a message back?
•
•
Join Date: Apr 2006
Posts: 4
Reputation:
Solved Threads: 0
i do not know if i understand it properly but,
both computers have to run server and client application in order to communicate equally.
also if you implement a multithread server, you will be able to handle multiple clients. ie. computer1 acts as a server, computer2 and other computers that are running client application may interact with server.
both computers have to run server and client application in order to communicate equally.
also if you implement a multithread server, you will be able to handle multiple clients. ie. computer1 acts as a server, computer2 and other computers that are running client application may interact with server.
![]() |
Similar Threads
- ssh server in java (Java)
- how to create smtp server java (Java)
- Detecting boot failures on the clients from the server (Networking Hardware Configuration)
- mssql server doesn't open port 1433 but open other ports that communicate with the DN (MS SQL)
- Dots and Boxes Client/Server in java (Java)
- help me to create a java chat server (Java)
- Custom Web Server Control That Validate Itself (ASP.NET)
- accessing clent bios from the server (Java)
- ASP, Java and XML-RPC (ASP)
Other Threads in the Java Forum
- Previous Thread: Help needed! Probably very basic!
- Next Thread: J2SE bluetooth server application
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






