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;
}
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(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);
}
Thanks in advance.