 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
lgnr
Joined: 17 Dec 2009 Posts: 39
|
Posted: Thu Dec 17, 2009 3:06 am Post subject: Sockets - weirdest thing ever |
|
|
Hello guys, i've already done every suggestion in this forum. No success. Well, kinda.
Base on the samples/net/simple i was able to code a simple socket client. It worked very well in the begining, but now sometimes it works and sometimes dont. I've already tried different ports and ips, and tried with NoBlock condition. The trick part is that SOMETIMES it works and sometimes dont.
I would really appreciate some help.
Code for psp:
| Code: |
int create_client()
{
int create_socket;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
int i = 0;
create_socket = socket(AF_INET,SOCK_STREAM,0); // TCP
address.sin_family = AF_INET;
address.sin_port = htons(PORT);
printf("\nConnect to port: %d\n", PORT);
inet_pton(AF_INET,"192.168.0.111",&address.sin_addr); // This is my server's addr
if(sceNetInetConnect(create_socket,(struct sockaddr*)&address,sizeof(address)) == 0)
printf("\nConexao realizada com sucesso com o servidor %s...\n",inet_ntoa(address.sin_addr));
else
{
printf("\nNao foi possivel conectar ao servidor\n");
return -1;
}
return 0;
}
|
And for the server (I got it from a tutorial, because its not my point right now):
| Code: |
main()
{
int cont,create_socket,new_socket,addrlen;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
printf("\x1B[2J");
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The socket was created\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(23);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("Binding Socket\n");
listen(create_socket,3);
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
if (new_socket > 0){
printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
for(cont=1;cont<5000;cont++)
printf("\x7");
}
do{
printf("Message to send: ");
gets(buffer);
send(new_socket,buffer,bufsize,0);
recv(new_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
}while(strcmp(buffer,"/q")); //user q to quit
close(new_socket);
close(create_socket);
}
|
really, ANY help will do.
Thanks in advance. |
|
| Back to top |
|
 |
lgnr
Joined: 17 Dec 2009 Posts: 39
|
Posted: Thu Dec 17, 2009 7:54 am Post subject: |
|
|
This is still puzzling me.
I took a look on both pspssh and pspftp sources, but wasnt able to figure what my problem is. I do not want to copy those codes, i just want to get a really simple socket like this to work.
Any guess? |
|
| Back to top |
|
 |
anmabagima
Joined: 01 Oct 2009 Posts: 96
|
Posted: Thu Dec 17, 2009 6:00 pm Post subject: |
|
|
Hi,
I'm uncertain about your client code but your Server seem not to accept any calls.
You are calling "listen" to switch to listen mode, but this would not "hold" your thread until a client connects. You need to do a loop where you are checking whether a client has connected or not.
Something like:
| Code: | while(true)
{
// accept() holds the thread until at least one client has connected
socketConnect = accept(socketAccept, NULL, NULL);
}
|
once the connection was established you could start communicating with the client. As you may connect to different clients you may think of handling each client communication in a different thread. However,
the simple way would be:
| Code: | if (socketConnect != SOCKET_ERROR) {
//get data sent by client
recv(socketConnect, buf, 255, 0);
//sent data back
send(socketConnect, "This is the answer from the server", 34, 0);
//if nothing more is needed close the connection
closesocket(socketConnect);
} |
|
|
| Back to top |
|
 |
lgnr
Joined: 17 Dec 2009 Posts: 39
|
Posted: Fri Dec 18, 2009 10:39 am Post subject: |
|
|
Thank you very much!
I think now it'll be ok.
Just another question: once i connect, i want to send the string "Square was pushed" when i press the button. But the server never gets it.
PSP:
[code]
while(1)
{
//Read
sceCtrlReadBufferPositive(&controller, 1);
if(controller.Buttons & PSP_CTRL_SQUARE)
{
printf("\nSQUARE!!");
send(create_socket,"\nSQUARE!",bufsize,0);
}
[/code]
Server:
[code]
do{
recv(new_socket,buffer,bufsize,0);
printf("Message recieved: %s\n",buffer);
}while(1);
[/code]
It prints "SQUARE" on psp but the server dont get it. Any toughts?
Thanks! |
|
| Back to top |
|
 |
anmabagima
Joined: 01 Oct 2009 Posts: 96
|
Posted: Fri Dec 18, 2009 5:41 pm Post subject: |
|
|
Hi,
it's difficult to solve. The first question was: does the client really connect ?
If the is the case you can check further, but please bare in mind that the repeated call of recv(....) within your loop wouldn't help unless you have the accept called before. I'm not 100% certain, but I guess that this is not a "stateful" connection. This would mean you need to call accept as well as recv within a loop, as the client will setup each time you send data a new connection to your server. In addition you should leave the "\n" and give him a try.
Is your Server running on PC ?
If done the following on my PC (with MinGW):
| Code: |
//============================================================================
// Name : SocketServer.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Socket Demo programm
//============================================================================
#include <iostream>
#include <stdlib.h>
#include <winsock2.h>
using namespace std;
SOCKET serverSocket, connectSocket;
SOCKADDR_IN serverAddress;
char buff[50];
int main() {
/*
* initialize the socket and set it to listen mode
*/
WSADATA wsa;
if (WSAStartup(MAKEWORD(1,1),&wsa))
cout << "WSA Init failed" << endl;
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.S_un.S_addr = ADDR_ANY;//inet_addr("127.0.0.1");
serverAddress.sin_port = htons(8099);
if (bind(serverSocket, (sockaddr *)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
cout << "Bind failed!" << endl;
if (listen(serverSocket, 5) == SOCKET_ERROR)
cout << "listen failed" << endl;
do {
connectSocket = accept(serverSocket, NULL, NULL);
if (connectSocket){
recv(connectSocket, buff, 50, 0);
cout << buff << endl;
send(connectSocket, "Hello", 6, 0 );
closesocket(connectSocket);
}
} while (1);
return 0;
}
|
if you start this on your PC you can also test your server there. Open a command prompt and type
telnet localhost 8099
If you than press any key, your server will print this key and the console will output hello. After this the connection will be closed. You can repeatly call telnet until you end your server.
If this works than try the connection from your PSP..
Regards |
|
| Back to top |
|
 |
lgnr
Joined: 17 Dec 2009 Posts: 39
|
Posted: Mon Dec 21, 2009 6:12 am Post subject: |
|
|
Thanks very much! Now its working!
I'll try to re-code it in a clearer way, and then i'll put the source here. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|