Sockets - weirdest thing ever

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
lgnr
Posts: 12
Joined: Thu Dec 17, 2009 2:57 am

Sockets - weirdest thing ever

Post by lgnr »

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: Select all

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: Select all

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&#40;cont=1;cont<5000;cont++&#41;
   printf&#40;"\x7"&#41;;
   &#125;
   do&#123;
      printf&#40;"Message to send&#58; "&#41;;
      gets&#40;buffer&#41;;
      send&#40;new_socket,buffer,bufsize,0&#41;;
      recv&#40;new_socket,buffer,bufsize,0&#41;;
      printf&#40;"Message recieved&#58; %s\n",buffer&#41;;
   &#125;while&#40;strcmp&#40;buffer,"/q"&#41;&#41;; //user &#145;q&#146; to quit
   close&#40;new_socket&#41;;
   close&#40;create_socket&#41;;
&#125;

really, ANY help will do.
Thanks in advance.
lgnr
Posts: 12
Joined: Thu Dec 17, 2009 2:57 am

Post by lgnr »

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?
anmabagima
Posts: 87
Joined: Thu Oct 01, 2009 8:43 pm

Post by anmabagima »

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: Select all

while&#40;true&#41;
    &#123;
        // accept&#40;&#41; holds the thread until at least one client has connected
        socketConnect = accept&#40;socketAccept, NULL, NULL&#41;;
   &#125;
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: Select all

if &#40;socketConnect != SOCKET_ERROR&#41; &#123;
//get data sent by client
recv&#40;socketConnect, buf, 255, 0&#41;;
//sent data back
send&#40;socketConnect, "This is the answer from the server", 34, 0&#41;;
//if nothing more is needed close the connection
closesocket&#40;socketConnect&#41;;
&#125;
lgnr
Posts: 12
Joined: Thu Dec 17, 2009 2:57 am

Post by lgnr »

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!
anmabagima
Posts: 87
Joined: Thu Oct 01, 2009 8:43 pm

Post by anmabagima »

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: Select all

//============================================================================
// Name        &#58; SocketServer.cpp
// Author      &#58; 
// Version     &#58;
// Copyright   &#58; Your copyright notice
// Description &#58; Socket Demo programm
//============================================================================

#include <iostream>
#include <stdlib.h>
#include <winsock2.h>
using namespace std;

SOCKET serverSocket, connectSocket;
SOCKADDR_IN serverAddress;
char buff&#91;50&#93;;

int main&#40;&#41; &#123;
	/*
	 * initialize the socket and set it to listen mode
	 */
	WSADATA wsa;
	if &#40;WSAStartup&#40;MAKEWORD&#40;1,1&#41;,&wsa&#41;&#41;
		cout << "WSA Init failed" << endl;
	serverSocket = socket&#40;AF_INET, SOCK_STREAM, 0&#41;;
	memset&#40;&serverAddress, 0, sizeof&#40;serverAddress&#41;&#41;;
	serverAddress.sin_family = AF_INET;
	serverAddress.sin_addr.S_un.S_addr = ADDR_ANY;//inet_addr&#40;"127.0.0.1"&#41;;
	serverAddress.sin_port = htons&#40;8099&#41;;
	if &#40;bind&#40;serverSocket, &#40;sockaddr *&#41;&serverAddress, sizeof&#40;serverAddress&#41;&#41; == SOCKET_ERROR&#41;
		cout << "Bind failed!" << endl;

	if &#40;listen&#40;serverSocket, 5&#41; == SOCKET_ERROR&#41;
		cout << "listen failed" << endl;

	do &#123;
	 	connectSocket = accept&#40;serverSocket, NULL, NULL&#41;;
	 	if &#40;connectSocket&#41;&#123;
	 		recv&#40;connectSocket, buff, 50, 0&#41;;
	 		cout << buff << endl;
	 		send&#40;connectSocket, "Hello", 6, 0 &#41;;
	 		closesocket&#40;connectSocket&#41;;
	 	&#125;

	&#125; while &#40;1&#41;;

	return 0;
&#125;
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
lgnr
Posts: 12
Joined: Thu Dec 17, 2009 2:57 am

Post by lgnr »

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.
Post Reply