regarding writing with TCP/IP in symbian - c++

void CSocket::WriteSocket()
{
TBuf8<2> KData (_L8("60"));
//RBuf8 KData;
RBuf8 aQuery;
aQuery.CleanupClosePushL();
aQuery.CreateL(100);
// <?xml version="1.0"?><AM><P/><A><CE/></A></AM>
_LIT8(KData1,"61");
//_LIT8(KData2,"20");
_LIT8(KData3,"A");
_LIT8(KData4,"<?xml version=\"1.0\"?><AM><P/><A><CE/></A></AM>");
TBuf8<100> buff,buff1;
buff.Copy(_L("<?xml version=\"1.0\"?><AM><P/><A><CE/></A></AM>"));
TInt len=buff.Length();
buff1.AppendNum(len);
aQuery.Append(KData1);
aQuery.Append(buff1);
// aQuery.Append(KData2);
aQuery.Append(KData3);
aQuery.Append(buff);
//iSocket.Send(KData,KExpeditedDataOpt,iStatus);
iSocket.Write((TDesC8)aQuery,iStatus);
User::WaitForRequest(iStatus);
}
I am using this code on Symbian for communication with server which is in Java.
But the issue is: the data is not reaching the server. It is showing device has succesfully connected. What i am doing wrong in this code? Is TDes8 compatible with plain text in Java?

TDes8 is just bytes, which is fine if the Java end is trying to read ASCII, not so fine if the Java end is expecting 16bit unicode (which is what Java's char is). But if you're reading the socket at the other end, then even if it wasn't compatible you'd still see some data, just not what you were expecting. And the protocol by which you're communicating with the server should specify the charset, regardless of what language the server is implemented in.
Otherwise:
Does it work on the emulator?
Have you checked iStatus for errors? Normally if a socket connects you can write to it, but you never know.
Is the server reporting that the socket is connected? It's possible you've connected to the wrong host or the wrong port.
If the server did read some data, but then failed for some reason, would you know? I guess I'm asking whether you're debugging the other end too. If not then it's possible your data isn't in the right format and is being ignored at the other end. I think you're sending 6146A<?xml version="1.0"?><AM><P/><A><CE/></A></AM>. Some tracing and/or packet sniffing will tell you whether that's true.
A variable starting with "a" is usually a parameter: is this your real code? If not, then the thing about the malformed data applies, and your caller might be giving you the wrong thing.
You might want to PopAndDestroy aQuery before return, although that doesn't affect this issue.
Your function should be called WriteSocketL, since it can leave.

cross-posting reply from duplicated question:
Your mobile network operator could be blocking any non-HTTP traffic.
Your server could need to receive more data before returning it all.
I'm also particularly concerned about your use of java character/string considering I would expect a low-level java socket on the server to put incoming network data in a byte[], not a String. If your server is using something like a call to a readLine() method, you may need to add a carriage return character in the data your client sends.

Related

C++ Sockets Unsynched/Parallel Code without Multithreading

I have this 2-side(client and server) program written in c++, which is basically a "chat program".
So one side sends data to the other side which sends data to the other side .
I am using C-Sockets (TCP/SOCK_STREAM).
So my Problem is, that while the server is waiting for data ("recv()") it is unable to send data itself, as the recv queue is infinite, as far as my experience shows.
I have thought of two ways to solve this, but I am not sure if its worth the time. Because there maybe is a simple solution, which I am just too stupid too think of.
Make it a 6-sided connection, meaning I have a "physical Server", where my server is running on in four instances, twice for each client(sending and recieving). so if one server recieves data it stores the data in a file, for the sending server of the other client to read out and.. send it. Works same the other way around.
As this solution sounds rather shitty, I might not go with it.
Using Multithreading. Which is as I have heard pure cancer to use and you shouldn't use it, unless you have to.
So my question is, do I have to? or is there maybe a simple solution?
recv() on a blocking socket is used when you need to wait for incoming data and have nothing to do until data is arrived. If it's not your case you can use non-blocking socket, or select with timeout. select would be simpler, just check if anything arrived on your socket regularly (e.g. in a loop), and do what you want in between.
By the way, it's called "BSD socket", not "C socket", this can help you googling.

Socket programming beginners questions

I'm really new to this whole socket and server development, I'm not yet familiar with how it all works.
I made a simple flash application that needs to communicate with a socket,
With that, I used a socket that supports AS3 and works on "Red Tamarin",
Well I'll get to the point:
I currently have a loop that always runs socket.receive()
It responds and even displays text that I send from my flash application.
My goal is to get a simple online flash game,
Probably use SQL / SQLite to save information and export it to players,
What I don't understand is how I can take it there..
What I thought I'll need to do is something like so:
On the server side:
Have a loop that runs as long as the server is alive, that loop should always check every connection it has with clients and wait for commands coming from them, such as log in, update player position, disconnect, request list of objects in given positions
Client side:
Send information to the server according to the action, like when a player moves, send the new position to the server in a similar way to this : "MovePlayer[name][x][y]"
Is my plan really how things should be?
And about the actual information being sent, I'm curious, will it be efficient to constantly send the server string data? (that's what I'm used to work with, not some weird bytes and stuff)
Thanks in advance!
You're on the right track. But I encourage you to first define a communication protocol. You can start by defining what a command looks like. For example:
COMMAND <space> PARAM1 <space> PARAM2 <line-break>
A few considerations on the protocol definition:
What if PARAM1 is a string and contains spaces? How can you tell the start and end of each parameter?
Your parameters could also contain a line-break.
If your client application is installed by your clients, they'll need to update it once in a while. To complicate even further, they may run an older version and expect it to work, even if you have changed your protocol. This imposes a need for protocol versioning. Keep that in mind if you require user interaction for updating the client part of your application.
These are the most fundamental considerations I can think for your scenario. There may be other important considerations, but most of them depend on how your game works. Feel free to amend my list if you think I forgot something OP should consider.
After defining what a command looks like, document all commands you believe your applications needs. Don't segregate definition of a command unless it becomes too complex or excessively long for some of your operations. Try to keep things simple.
Now back to your questions:
Is my plan really how things should be?
Yes. That's exactly how it should be.
And about the actual information being sent, I'm curious, will it be efficient to constantly send the server string data? (that's what I'm used to work with, not some weird bytes and stuff)
That depends on a number of factors:
Which protocol you're using (TCP, UDP, etc);
Number of concurrent clients;
Average time to process a command;
Do you broadcast updates to other players?
How you did implement your server application;
Physical contraints:
Hardware: CPU, memory, etc;
Network: bandwidth, latency, etc;
(source: it20.info)
look at this
https://code.google.com/p/spitfire-and-firedrop/
there you will see the basic of building a socket server with redtamarin
see in particular
https://code.google.com/p/spitfire-and-firedrop/source/browse/trunk/spitfire/src/spitfire/Server.as
the details is as follow, redtamarin basically use blocking sockets with select()
with a max hard coded FD_SETSIZE of 4096
see:
https://code.google.com/p/redtamarin/wiki/Socket#maxConcurrentConnection
so here what happen in your server loop
you basically have an array of sockets object
you loop every x milliseconds and for each socket
you ask if you can read it
if you can read on the socket, you then compare if this socket obj is the server
if it is the server that means you have a new connection
if not that means a client try to send you data and so you read this data
and then pass it to an "interpreter"
later in the same loop you check if the socket obj is still valid
and if you can write to it
if you can write and the socket object is not the server
then you can send data to the client
here the equivalent code in C for reference
http://martinbroadhurst.com/source/select-server.c.html
http://www.lowtek.com/sockets/select.html
for a very basic example look at socketpolicyd
https://code.google.com/p/spitfire-and-firedrop/wiki/socketpolicyd
https://code.google.com/p/spitfire-and-firedrop/source/browse/trunk/socketpolicyd/src/spitfire/SocketPolicyServer.as
and compare the implementation with Perl and PHP
http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html

how do I connect a socket to a modem connection?

All,
I am working on an embedded linux application that is to use a cellular connection to communicate with a database.
I know that I can use AT commands to create socket to the server, but how do I get access to the socket from a C++ program? That is, after I issue the AT commands, how do I use it from an application?
Sorry if this is a stupid question, but I can't find an answer...
Thanks for all your help!
:bp:
Check the documentation for your modem. Multitech has one online here for their modems which may or may not be helpful (if yours is compatible).
Generally, after doing the WIPCREATE, you need to wait for a WIPREADY or WIPACCEPT from the modem; you can then do WIPDATA to put your connection to the modem into data mode, at which point everything you write will be sent to the socket, and anything received on the socket will be sent back to you (so you can treat the fd connected to the modem as if it was a socket, you just need to be careful not to send escape sequences accidentally -- or at least escape the escape sequences properly).
That depends on your OS. On most normal Unix OS's you can simply use /dev/tty*, open it, set the baudrate etc and issue AT commands.

using libevent to read a continuous http stream and sending data at random times

Firstly I think I need to say that I'm still learning C++ so apologies if this is blindingly obvious/simple.
I'm trying to use the libevent library (by trying I've looked through code in the sample folder and tested some) in my C++ program to consume an http stream. I'm wondering if anyone can provide me with an example of how I'd go about connecting to a URL e.g. live.domain.com, sending the appropriate headers, read the data returned and send data back over the same connection...
I'm not sure libevent does any blocking connections but just to be explicit, I'm after non-blocking samples.
Why am I trying to do this?
I'm using an API which requires you to open a connection and it keeps it alive unless there's an error. It'll periodically send status texts to the connected client until it receives a string with an ID over the same connection. At which point it starts sending data back about the ID given... I'm not entirely sure sending data back over the same connection after the initial request is strictly compliant but that's what the server expects so it'll work...if I knew how
Thanks in advance
Yuck. Given that this isn't really HTTP, I don't think you're going to be happy using a HTTP library - even if you get it to work today after a lot of frustration, it could easily be broken tomorrow. This is too rare to be a supported feature.
But...it sounds like it's also simple enough that you could just open a raw TCP connection with libevent, manually send something that looks kind of like an HTTP request, and handle it with raw sockets from there. You don't want the extra stuff a HTTP library gets you anyway (additional transfer/content encodings, proxy support, SSL, compatibility with other protocol versions, ...)
As far as examples go, look at the libevent book. In particular, the a "Trivial HTTP v0 client" that seems very close to what you want. Good luck!

TCP Connection Hijacking

I have a small project that I've been working on in C++, and due to the nature of what it does, I need to insert packets in to a live TCP stream. (The purpose is innocent enough, http://ee.forumify.com/viewtopic.php?id=3299 if you MUST know)
I'm creating a level editor for a game, and due to the nature of the handshakes, I can't simply establish a new connection with a high level library such as WinSock. Until now, it has relied on Winsock Packet Editor to do the dirty work, but if I were to let the application handle it all, it would make everyone happy.
So my question is this: Is there an API somewhere that will allow me to take control of a live TCP stream, and preferably one that keeps it valid after it finishes? And I would prefer to not have to inject any DLLs. Also, Detours is a no-no as I'm using GCC/Mingw.
I've toyed around with WinPCap and I have some working code (I can collect a packet, and from that generate a proper packet to send) but since it operates at such a low level, I cannot anticipate all of the potential protocols that the end user might use. Yes, chances are that they'll be using IPv4 over Ethernet, but what about those people who still use PPP, or some other obscure protocol? Also, the connection gets dropped by the client application after mine is done with it, as the latest ID values in the packets have changed and the client assumes that it has disconnected.
So, if anyone could provide a high-level TCP stream manipulator, I would be very happy. If not, I'll just continue tinkering with WinPCap and tell all the dial-up users to go get better internet.
Target platform: Microsoft Windows XP through Windows 7
Create a separate process to bind to a local port. When the initial tcp stream is created, proxy it through that process, which can then forward it on to the network. When you need to 'inject' into the stream you can have this proxy process do it. Just a thought.
you should look at the source code of ettercap http://ettercap.sourceforge.net/
or hunt, tcp hijacker http://packetstormsecurity.org/files/view/21967/hunt-1.5.tgz
Those 2 softs do what you're after.
I don't think there's any sensible API that will allow you to hijack a TCP stream. Such a thing would, inherently, be a security problem.
Can you insert your program as a proxy for the connection in question? That is, get the program that opens the connection to open it to your program, then have your program open the connection to the real target.
The idea is that if all the packets pass through your program anyway, then modifying the TCP stream becomes relatively trivial.