Sockets work on localhost but not remotely?

hi there,
I have created a simple multithreaded client / server program.
The Server listens on port 2222 for clients. When a client connects - the client sends its ip address to the server and then disconects. More than 1 client can connect at the same time to the server and the server also listens continuasly. I have 2 problems with the program...
a) the programs work on localhost but not on remote machines? (Well not over my LAN anyway).
b) I am getting 2 lots of 'datain' when the server recieves an ip address from the client when the client should only send it once.
The code is below - any help would be greatly appreciated.
//TCPServer.java
import java.io.*;
import java.net.*;
class TCPServer {
     public static void main (String args[]) throws IOException {
          ServerSocket serverSocket = null;
          boolean listening = true;
          try {
               serverSocket = new ServerSocket(2222);
          } catch (IOException e) {
               System.err.println("Could not listen on port: 2222");
               System.exit(-1);
          System.out.println("Server Started...\n");
          while (listening)
          new TCPServerThread(serverSocket.accept()).start();
          serverSocket.close();
//TCPServerThread.java
import java.net.*;
import java.io.*;
public class TCPServerThread extends Thread {
    public Socket socket;
    public TCPServerThread(Socket socket) {
     super("TCPServerThread");
     this.socket = socket;
    public void run() {
               try {
               BufferedReader datain = new BufferedReader (new InputStreamReader
                    (socket.getInputStream()));
                    System.out.println("ip address recieved");
                    System.out.println (datain.readLine () + "\n");
               } catch (IOException e) {
                    System.err.println("Cannot read in ip address\n");
                    e.printStackTrace(); // show the error
                    System.exit(-1);
     } //TCPClient.java
import java.io.*;
import java.net.*;
class TCPClient {
     public static void main (String args[]) throws Exception
          String hostname;
          int portNumber;
          String portString;
          BufferedReader inFromUser =
               new BufferedReader (new InputStreamReader(System.in));
               System.out.println("What host would you like to connect to?");
               hostname = inFromUser.readLine();
               System.out.println("What port would you like to connect to?");
               portString = inFromUser.readLine();
               portNumber = Integer.parseInt(portString);
               System.out.println("Connecting to port " + portNumber + " of " + hostname + "....\n");
                    Socket clientSocket = new Socket(hostname, portNumber);
                    Socket sock = new Socket (InetAddress.getLocalHost(), portNumber);
                    BufferedWriter dataout;
                    java.net.InetAddress i = java.net.InetAddress.getLocalHost();
                    System.out.println("Sending ip address...\n" + i.getHostAddress());
                         dataout = new BufferedWriter (new OutputStreamWriter (sock.getOutputStream()));
                              dataout.write (i.getHostAddress());
                              dataout.flush();
                              sock.close();
                                   clientSocket.close();
                         }Oh, yeah - check out my site below, I am trying to write an audio streaming client / server in Java if anyone is interested in getting involved or just being nosey....
http://www.projectg.dsl.pipex.com

In your client you are creating two sockets (why?). One connected to localhost, the other connected to the host specified using readLine.
If your client runs on the same machine you have started your server, you have two connects to your server instead of one.
The ip adresse is sent using the socket connected to localhost. That is why it does not work on remote machines.
Hope this helps.
Jörg

Similar Messages

  • Slideshow works on local but not remote server?

    Slideshow works on local but not remote server? The site is patriceclarkson.com.

    When I compare your:
    http://www.warpd.ca/SpryAssets/SpryWidget.js
    to this one:
    http://labs.adobe.com/technologies/spry/ui/includes/SpryWidget.js
    I do not see the same file. Try uploading the correct file and check again.
    Xav

  • Tomcat 5.5, struts, AJAX - Works on localhost but not on online web-server

    Hello,
    I'm trying to add some functionality from Ajax to my client's web application. And I'm stuck on this problem for the past 4 days now.
    To make a long story short, the application works perfectly on my localhost, but once I deploy it to the online server it pops a Page Not Found message (written in the JavaScript of Ajax engine). Though I have worked with servlets and struts a bit, I still consider myself very new.
    Here are some specifications:
    - The jar files are in tomcat-5.5/common/lib
    - The servlets (compiled correctly) are in webapps/ROOT/WEB-INF/classes/javaClasses
    - My web.xml and struts-config.xml are in WEB-INF, both of which I believe are configured correctly:
    web.xml
    =======
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
    <web-app>
         <servlet>
              <servlet-name>action</servlet-name>
              <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
              <init-param>
              <param-name>config</param-name>
              <param-value>/WEB-INF/struts-config.xml</param-value>
              </init-param>
         </servlet>
         <servlet-mapping>
              <servlet-name>action</servlet-name>
              <url-pattern>*.do</url-pattern>
         </servlet-mapping>
         <welcome-file-list>
              <welcome-file>/index.jsp</welcome-file>
         </welcome-file-list>
    </web-app>
    struts-config.xml
    ==============
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
    <struts-config>
    <action-mappings>
    <action path="/signup" type="org.apache.struts.actions.ForwardAction" parameter="/signup.jsp" />
    <action path="/countries" type="javaClasses.GetCountries" />
    </action-mappings>
    </struts-config>
    I noticed when I type "countries.do" on my localhost, the action is processed perfectly, however on the online server, it displays me the 404 error page.
    I thought it wasn't seeing the struts-config.xml so I played around with the directory path struts-config.xml and replaced it with:
    <param-value>/home/shaghig/webapps/ROOT/WEB-INF/struts-config.xml</param-value>
    but still no luck.
    What is it that I am not doing right? Any input or guidance would be very much appreciated.

    Have you resolved your problem? I am running into the SAME EXACT issues with my application. The only difference is I'm not deploying to webapps/ROOT/; I'm deploying to my own directory based on my WAR file name (webapps/<web_application_name>/WEB-INF....blah blah). If you figured out your problem (believe me, I wish I could be of some help) I would LOVE to hear what you did/figured out.
    -C

  • MySQL error on localhost, but not remote!!

    I am getting an error of:
    Warning: mysql_free_result() expects parameter 1 to be resource, null given in /Users.... line 848
    But I am only getting this on my dev machine. It doesn't show on the remote site. I have taken the remote DB and put it inplace of my local one just in case I had inadvertantly deleted something, but still no joy.
    I have closed the recordset using:
    mysql_free_result($rs_reg_domain);
    But have had to use the following to hide the error:
    //if (is_resource($rs_reg_domain)) mysql_free_result($rs_reg_domain);
    and finally this is the recordset:
    mysql_select_db($database_conn_mrs, $conn_mrs);
    $query_rs_domain = "SELECT * FROM tbl_settings WHERE fld_settingsNAME = 'domain'";
    $rs_domain = mysql_query($query_rs_domain, $conn_mrs) or die(mysql_error());
    $row_rs_domain = mysql_fetch_assoc($rs_domain);
    $totalRows_rs_domain = mysql_num_rows($rs_domain);
    'domain' has a value.
    What's wrong?
    Thanks

    The reason you get an error locally, but not on the remote server is almost certainly because the remote server has the PHP display_errors configuration setting turned off. The error still occurs, but it's not displayed. Most hosting companies turn off the display of errors for security reasons.
    Looking at the code you have given here, it looks as though the error is caused by using the wrong variable name for the recordset. The recordset is $rs_domain, but you're passing $rs_reg_domain to mysql_free_result().

  • AMF work in Localhost, but not work in Remote server

    I am using zend's amf to connect the flex with backend.
    I have this service-config:
    <?xml version="1.0" encoding="UTF-8"?>
    <services-config>
        <services>
            <service id="zend-service"
                class="flex.messaging.services.RemotingService"
                messageTypes="flex.messaging.messages.RemotingMessage">
                <destination id="zend">
                    <channels>
                        <channel ref="zend-endpoint"/>
                    </channels>
                    <properties>
                        <source>*</source>
                    </properties>
                </destination>
            </service>
        </services>
        <channels>
        <channel-definition id="zend-endpoint" class="mx.messaging.channels.AMFChannel">
            <endpoint uri="http://www.domain.com.br/flex" class="flex.messaging.endpoints.AMFEndpoint"/>
        </channel-definition>
        </channels>
    </services-config>
    When I run my app in localweb this work nice, but when I upload my app to the host, this erro show:
    [RPC Fault faultString="Send failed" faultCode="Client.Error.MessageSend" faultDetail="Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Failed: url: 'http://www.domain.com.br/flex'"]
        at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()[C:\autobuild\3.x\frameworks\pro jects\rpc\src\mx\rpc\AbstractInvoker.as:290]
        at mx.rpc::Responder/fault()[C:\autobuild\3.x\frameworks\projects\rpc\src\mx\rpc\Responder.a s:58]
        at mx.rpc::AsyncRequest/fault()[C:\autobuild\3.x\frameworks\projects\rpc\src\mx\rpc\AsyncReq uest.as:103]
        at mx.messaging::ChannelSet/faultPendingSends()[C:\autobuild\3.x\frameworks\projects\rpc\src \mx\messaging\ChannelSet.as:1446]
        at mx.messaging::ChannelSet/channelFaultHandler()[C:\autobuild\3.x\frameworks\projects\rpc\s rc\mx\messaging\ChannelSet.as:1056]
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at mx.messaging::Channel/connectFailed()[C:\autobuild\3.x\frameworks\projects\rpc\src\mx\mes saging\Channel.as:1069]
        at mx.messaging.channels::PollingChannel/connectFailed()[C:\autobuild\3.x\frameworks\project s\rpc\src\mx\messaging\channels\PollingChannel.as:388]
        at mx.messaging.channels::AMFChannel/statusHandler()[C:\autobuild\3.x\frameworks\projects\rp c\src\mx\messaging\channels\AMFChannel.as:411]
    Please, somebody can help-me, please?

    RESOLVED!
    The problem was on the server: http resquest return 301... I turn off the "mod_security" on the server and all works perfect now!

  • Can connect to MySQL via localhost, but not remotely!

    Hi all!
    I got a problem that I think you guys could help me with :)..
    I have installed JDBC to make it possible to link my Java programs with MySQL databases. The installation worked just fine.
    When I'm running my program with line:
    Connection connection = null;
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/chat", "*****", "*****");...it works without any problems. I can handle information from the tables, add posts, delete posts and so on.
    But when I'm trying to connect from an another computer, from school for an example, it doesn't connect. JBDC says it can't fint the mysql jbdc driver.
    When I'm using this way I uses the line:
    Connection connection = null;
    connection = DriverManager.getConnection("jdbc:mysql://85.235.154.98:3306/chat", "*****", "*****");Like I said, this doesn't work at all.
    How do I fix this problem?
    Worth to mention maybe, is that I'm using Windows XP Home, and I have placed the mysql-connector-java-5.0.5-bin.jar in "C:\Program\Java\jre1.6.0_01\lib\ext"
    I'm using Java version 1.6.0!
    Please Help :)

    But when I'm trying to connect from an another
    computer, from school for an example, it doesn't
    connect. So the database is on your home computer? And you are attempting to connect from the school?
    JBDC says it can't fint the mysql jbdc
    driver.Then it has nothing to do with connecting. That means your class path is wrong.
    If it actually is a connection problem then you can do the following in a console window....
    telnet 85.235.154.98 3306
    There will be one of two results from the above after several minutes.
    1. It will just sit there.
    2. It will tell you it can't connect.
    For 2 you have a problem that has nothing to do with java. Looking at your code will not fix it.
    If it is 1 then something is wrong with your code and you should provide the exact exception.

  • Tiger Server: Default MySQL works for phpMyAdmin but not command line ? ? ?

    Greetings all,
    I followed the excellent instructions at
    http://discussions.apple.com/thread.jspa?threadID=132783&tstart=0
    on upgrading PHP to version 5 and getting (the preinstalled) MySQL setup with phpMyAdmin
    I can connect through the phpMyAdmin, create new dbs, etc. Problem is that now I CAN'T connect from the command line. (I originally set my new root password from the command line and that's the last time I could connect from there!
    Now I get the dreaded ERROR 2002 when I attempt:
    /usr/bin/mysql -h localhost -u root -p
    If I use the IP address or hostname instead of "localhost" (after -h) I get a ERROR 2003. Dropping the -h and hostname from the line above still gets me a ERROR 2002.
    My phpMyAdmin config is a connect type "TCP" not socket, which might be one reason it works and the command line doesn't, but I still don't know where I've gone wrong.
    My /var/mysql/* is all owned by mysql and group www.
    I'm finding lots of people asking about the ERROR 2002, but not finding any solutions (particularly for Tiger Server). I'd prefer to use the preinstalled MySQL to installing a new installation (the thread referenced above says upgrading or removing the MySQL installation is too hard to do) and there has GOT to be a reasonable explanation for why it works with phpMyAdmin, but not the command line.
    Thanks in advance for any replies!
    Many   Mac OS X (10.4.3)  

    You know what? I think when I started mysql_safe, I did it with sudo. Maybe that is my problem?
    If so, what user do I want to start it as? I've seen one suggestion of doing this:
    ./bin/mysqld_safe --user=mysql &

  • Custom F4 help in a BSP application working in Mozilla but not in IE

    Hi,
    I have created a custom F4 help in a BSP application.
    It is working in Mozilla but not in IE. I tried enabling javascript as well but still it is not working.
    Can someone please suggest a possible solution for this?

    Hi,
    See simmilar discussion in thread /thread/1883519 [original link is broken]
    I think this may be helpful for you.
    Thanks,
    Chandra

  • Why does the counter work in MAX, but not Labview?

    Please help, I have been away from Labview for over 3 months, and I am quite rusty....
    Last year, I successfully configured NI9401 to measure period (seconds) by counting the rising edges of my sensor signal.  Since then, I have aquired a new PC, and loaded all my software into it, including the Labview VI's that successfully worked on the old computer.
    I can see the digital signal using MAX and the test panel for the NI9401, so, I know my wiring and my TTL signal is OK, but, when I try to run the attached VI, nothing happens except the error below..  How do I tell this VI to look for the signal on MOD3/CTR0 and channel 14?
    Thanks. I am sure it is a small adjustment...this worked fine a few months ago...not sure what is wrong now.
    Thanks,
    Dave
    Solved!
    Go to Solution.
    Attachments:
    continuously measure period buffered_singlechannel_forum_feb_23_2012.vi ‏20 KB

    OK, now.  Resolved the issue. The vi is fine, but, some shaky wires were identified...worked in MAX but not in the vi.  Now that the connections have been tightened up, it works for both.  Sorry for the inconvenience.
    Dave

  • Works on emulators but not on mobile devices

    Hello,
    My project works on emulators but not on mobile devices (no image and on Samsung it says "unsupported file").
    Please help, I don't know what to do. My configurations: MIDP 2.1, CLDC 1.1.
    Thanks in advance.
    Edited by: Vitali.pom on Oct 27, 2011 12:01 PM

    Edit: I succeeded to solve it accidentally. I did the following: Sign the jar in Netbeans, clean and build, the jar didn't work, I wiped out the .ks from the jar and clean and build again. Now it worked.
    Edited by: Vitali.pom on Oct 30, 2011 1:28 AM

  • Wifi works at home but not when away from home

    My Wifi works at home but not when away from home, what is wrong?  I thought with a phone plan I didn't need to have a wifi connection.

    You either need WiFi or Cellular (or both). If you have neither, you can't connect to the internet.
    Most people use WiFi at home and Cellular while away from home, or connect to public WiFi networks where there are some.

  • Can't FaceTime anymore?  iPad states wifi connected and iPhone 4 states not connected?  I have wireless in my home? Not sure what happened.  Password works for iPad but not on phone?

    iPhone won't connect to wifi.  iPad is connected.  Password working on iPad but not on phone.  Can't upload software update of IOS 7.1 because phone not connecting to wifi?

    Connect iPhone to computer and open iTunes.  You will be prompted to update to iOS 7.0.  Keep your phone connected until the update is finished.

  • My apple id works on itunes but not on my ipod touch, what gives ?

    my apple id works on itunes but not on my ipod touch, what gives ?

    What are you trying to do on the iPod?
    Have you went to Settings>Store and sign out and then sign back in?

  • Album Art works for most, but not all

    I was reading previous forums on the subject, but no clear answer was given..
    The album art shows up fine in itunes and my library and works for most of my music on my ipod, but how come some album's art does appear? I have it set manually and select 'Get Info' then 'Artwork' and add it from there, then drag in the finished product to my ipod. I've been doing that and it works fine for most of the albums, but every so often, the album art will not appear on my ipod. Am I doing something wrong? I take them all from the same source (amazon.com) but it makes no sense to me how some randomly don't appear.
    I tried deleting the artwork and re-adding it, but nothing happened again. If you could answer why it works for most but not all or how I can get the album art to appear for the ones it will not by re-doing something, it would be appreciated. thanks.

    I encountered the same problem in the beginning. When I compared the size of the original images, the working ones seemed to be smaller in general, so I reduced the size of all covers I intended to upload on the pod to 300x300 pixels.
    This seemed to work - for 2 days. I bought the pod on friday, had pictures on sunday, but on Monday (after the first time the pod was fully discharged) none were left left.
    So I ended searching this forum for related problems...
    And decided to join my problem to this entry.

  • How to send active links in email from firefox. Works in IE but not Firefox... Is there a setting to change?

    how to send active links in email from firefox. Works in IE but not Firefox... Is there a setting to change?
    == This happened ==
    Every time Firefox opened
    == Always

    Check with your web mail service provider for help with that issue.

Maybe you are looking for