loadPolicyFile doesnt work in AS3 - java

(Im bad in english i try to be good for a good explain)
i got a client socket in AS3 and a server in Java. In localhost, i got no problem to connect my client and my server. They can exchange data with no problem with this line :
socket.connect("127.0.0.1", 2030);
Its ok, my server can receive a Byte[] data , can read and write to my client with no problem.
But now i want to past the server "online" so i open the port 2030 for the connection and the 82 port, and i try to read the crossdomain.xml to be autorized, with :
Security.loadPolicyFile("http://90.20.233.143:82/crossdomain.xml");
socket.connect("http://90.20.233.143", 2030);
now when im start the connection ... have got some problem with Security.loadPolicyFile
Im getting on JAVA Server :
java.net.SocketException: Connection reset
And in Client As3 (in french):
Connexion au serveur.... Vous etes connecté au serveur
Avertissement :La balise non valide est ignorée
pour le domaine 'http://90.20.233.143' dans le fichier de régulation
présent à http://90.20.233.143:82/crossdomain.xml
Socket error: [IOErrorEvent type="ioError" bubbles=false
cancelable=false eventPhase=2 text="Error #2031: Erreur de socket.
URL: 90.19.160.185"] // its sur cause "connection reset" on JAVA
my code in my crossdomain.xml :
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="http://90.20.233.143/" to-ports="*"/>
<site-control permitted-cross-domain-policies="all" />
</cross-domain-policy>
i dont know where is the problem ...

It's a http server , http://90.20.233.143:82/crossdomain.xml , is path of crossdomain.xml.
The client can read the XML but he said that "marker" isnt good, so he will ignore the marker ("balise" in french). SO the plan is open port 843 on my http server ? Or java server ?

Ok ok so i convert the code in a Byte in XML file , and by the method Security.loadPolicyFile , i read the XML ?

Related

Connect java to sql server via JDBC [duplicate]

This question already has answers here:
The TCP/IP connection to the host localhost, port 1433 has failed error, need assistance
(4 answers)
Closed last month.
I try, without any success, to connect my java project to my sql server database (local on my laptop).
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JvaConnect2SQL {
public static void main(String[] args) {
String url = "jdbc:sqlserver://LAPTOP-0CSKUFIE\\MSSQLSERVER;databaseName=datatreck";
String username = "sa";
String password = "hello";
try {
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Connecté à SQL server");
} catch (SQLException e) {
System.out.println("Erreur - Problème lors de la connexion");
e.printStackTrace();
}
}
}
The error is :
com.microsoft.sqlserver.jdbc.SQLServerException: La connexion à l'hôte LAPTOP-0CSKUFIE, instance nommée mssqlserver, a échoué. Erreur : « java.net.SocketTimeoutException: Receive timed out ». Vérifiez le nom du serveur et celui de l'instance et veillez à ce qu'aucun pare-feu ne bloque le trafic UDP vers le port 1434. Pour SQL Server 2005 ou ultérieur, vérifiez que le service SQL Server Browser est en cours d'exécution sur l'hôte.
English Translation:
com.microsoft.sqlserver.jdbc.SQLServerException: Connection to host LAPTOP-0CSKUFIE, instance named mssqlserver, failed. Error: "java.net.SocketTimeoutException: Receive timed out". Check the server name and instance name and ensure that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser service is running. running on the host.
It seems the name of the host and/or instance are not right ><
but I was sure it was the good ones, so I'm confuse ...
How can I be sure of my host/instance names ?
Is the code all right ?
I add exception to the fire wall and add the jdbc driver.
I don't understand why the connexion fail.
Just a simple tip: don't code or install applications in your native language. There are many reasons for doing that, but I am not going to list them here.
If SQL server is running on your local computer/server requests should be made to localhost:port (for example localhost:1433)
Check an example connection string and steps to connect to the database here: https://stackoverflow.com/a/71481219/19879452

FTP to 1and1.com

The code below works to two other sites I've tried, but will not work with my domain hosted by 1and1. The return code is always 500 - Permanent Negative Completion reply.
I know I'm connecting because FTPReply.isPositiveCompletion(reply) returns true. I tried it with both reading file off phone storage and sending it from a byte array populated early in the code. This is the byte array. Both return 500. Both work on the other sites.
If I don't use enterLocalPassiveMode() the code stops executing on the storeFile call. No exception, no socket time-out. It just ends there and the async task will not call again in that session. The code does not do that on the other sites.
I've tried both ASCII and BINARY file types. Both return 500. 1and1 site says to use my domain and port 21. I can connect with CoreFTP and read and write using both of the accounts I've set up.
I also tired ftp4j and had the same response with all scenarios so went back to Apache because the code was already written with robust error trapping.
I've tried both mydomain.com and ftp.mydomian.com. 500 on both. I also tried the dot quad I can see in the CoreFTP window, but i get "cannot resolve host name" with the Apache Java code. Maybe not a static IP?
This is what CoreFTP does. It connects on port 21 and then goes in to passive mode and an ASCII data connection.
It's a long shot, but has anyone else ever FTPed to their 1and1 domain using Java in Android Studio?
Greg
Resolving mydomain.com...
Connect socket #5684 to xx.xx.xx.xxx, port 21...
220 Microsoft FTP Service
USER ftp79815757-0
331 Password required for ftp79815757-0.
PASS **********
230 User logged in.
SYST
215 Windows_NT
Keep alive off...
PWD
257 "/ftp79815757-0" is current directory.
PASV
227 Entering Passive Mode (xx,xxx,xx,xxx,xxx,xxx).
LIST
Connect socket #5700 to xx.xx.xx.xx, port 62894...
150 Opening ASCII mode data connection.
226 Transfer complete.
Transferred 51 bytes in 0.094 seconds
FTPClient mFtpClient = new FTPClient();
String ip = "my domain dot com";
String userName = "ftp79815757-0";
String pass = "password";
mFtpClient.connect(InetAddress.getByName(ip));
mFtpClient.login(userName, pass);
int reply = mFtpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
mFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
//one thread said this would do the trick
mFtpClient.enterLocalPassiveMode();
mFtpClient.enterRemotePassiveMode();
InputStream stream = new ByteArrayInputStream(imageData);
//I have two accounts. One points to images_in
/*if (!mFtpClient.changeWorkingDirectory("images_in")) {
Log.e("ChangeDir", String.valueOf(mFtpClient.getReplyCode()));
}*/
if (!mFtpClient.storeFile("remoteName.jpg", stream)) {
Log.e("FTPUpload", String.valueOf(mFtpClient.getReplyCode()));
}
stream.close();
mFtpClient.disconnect();
}
Finally got it. The main problem was that I was using an old version of the Apache library. The Jar I was using was commons-net-1.4.jar. Someone in another thread pointed me to commons-net-3.3.jar.
I commented out both mFtpClient.enterLocalPassiveMode() and mFtpClient.enterRemotePassiveMode(), and with some trail and error it worked with FTP.BINARY_FILE_TYPE and not ASCII_FILE_TYPE. ASCII got the file there, but it was garbage.

Using a socket for FTP server, but output is not received by client

I'm trying to create a very basic Java FTP server.
The code to handle the incoming socket, accept it, and turn it into a client thread seems to work great, but for some reason it doesn't look like data is getting TO the client when I try to send the welcome message.
// outputstreamwriter for socket
out = new OutputStreamWriter(socket.getOutputStream(),"UTF-8");
// function for sending data
private void send(String s) {
try {
out.write(s,0,s.length());
out.flush();
} catch (IOException e) {
System.out.println("Exception during send(): " + e);
}
System.out.println("> " + s);
}
I see in my console that it is running and trying to send the welcome message, however FileZilla (the FTP client I'm using to connect) usually sits at:
"Connection established, waiting for welcome message..."
===
Edit
The welcome message is pretty basic:
send("220 WELCOME TO THE FTP SERVER");
I'm trying to include only the relevant code because it's far too much to post here. (Listening server thread which launches each client thread, etc.) The point is that it's getting to this point in the code without any problems (i.e. client thread w/ accepted socket ready-to-go) and then when I send data FileZilla appears not to see it.
You need a CR/LF pair at the end of all communication lines. See RFC959:
The File Transfer Protocol follows the specifications of the Telnet
protocol for all communications over the control connection. Since
the language used for Telnet communication may be a negotiated option,
all references in the next two sections will be to the "Telnet
language" and the corresponding "Telnet end-of-line code". Currently,
one may take these to mean NVT-ASCII and <CRLF>. No other
specifications of the Telnet protocol will be cited.

PHP Socket Server for multiple clients [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A PHP Socket Server with Flash Clients
I am building an app in my server with the help of a flash developer, and he asked me to build a socket server to communicate with the database. He recommended me JAVA but I am not very good at JAVA and I was wondering if it was possible to build a Socket Server in PHP.
I should allow connections to multiple TCP client connections. I know in JAVA this is done thought threading, but I am not sure if this can also be achieved with PHP.
Could someone please show me the basic skeleton of a PHP Socket Server with these characteristics?
The connection has to be TCp (persistent) from the beginning of the connection to the app, until the end.
You have to run your socket server as a service from the command line.
This is a part of what I have used before. It closes the socket after a read but can easy be modified to keep a array of connections.
You would have to build some kind of watchdog to see if a connection is still alive.
You need a identifying mechanism to identify different connections.
The code:
set_time_limit( 0 );
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 6789;
// Create a TCP Stream socket
$sock = socket_create( AF_INET, SOCK_STREAM, 0 ); // 0 for SQL_TCP
// Bind the socket to an address/port
socket_bind( $sock, 0, $port ) or die( 'Could not bind to address' ); //0 for localhost
// Start listening for connections
socket_listen( $sock );
//loop and listen
while (true) {
/* Accept incoming requests and handle them as child processes */
$client = socket_accept( $sock );
// Read the input from the client – 1024000 bytes
$input = socket_read( $client, 1024000 );
// from here you need to do your database stuff
// and handle the response
// Display output back to client
socket_write( $client, $response );
socket_close( $client );
}
// Close the master sockets
socket_close( $sock );
There is a WebSocket Server and Client library for PHP At Google code . It supports flash clients . Not sure whether it solves your problem .
If you want a basic tutorial here is the link to learn
How to create a socket server in php
EDIT :- after looking at your comment ( running a socket server continuously as a service )
Here is the link which describes the way of creating a socket server and running as a process
Create a socket server in php and run as a service
Rather than building a "socket server", you should really build a set of web APIs (SOAP, REST/JSON, whatever) that provides limited and well-defined access to the DB. Then have the Flash app use that.
Your flash app sends JSON over a RESTful interface, or SOAP/XML requests. The server receives them, interacts appropriately with the database, and returns any required results again as XML or JSON.

Connect to Socket server from Actionscript 3 AIR application

I read through the method to connect to a socket server : http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_5.html
I wrote an AIR application to connect to the java server code, but the client cannot connect. What is missing in the code?
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" backgroundColor="#D5F8C3">
<fx:Script>
<![CDATA[
import flash.net.XMLSocket;
var socket:XMLSocket;
public function b1_clickHandler(event:MouseEvent):void
{
var socket:XMLSocket;
socket.connect("127.0.0.1",8080);
socket.send("Hello");
//socket.addEventListener(DataEvent.DATA, onData);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button id="b1" x="285" y="162" label="Send Text" width="201" height="105" click="b1_clickHandler(event)"/>
</s:WindowedApplication>
You have to wait for CONNECTED event before sending data. Also, be sure you've managed crossdomain security (look at : http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7c60.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7c63)
Server/client code exemple : Air 2 ServerSocket Crossdomain problem

Categories