Skip to: Site menu | Main content


Welcome to PSP-Programming.com, a place for developers to get together.

Welcome to the forums. Here you can find other user tutorials as well as homebrew releases and the source code repository. You can also ask for help with your code here and post your own homebrew!

PSP-Programming.com Forums
February 10, 2012, 02:02:18 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

News: Join our IRC channel: ##psp-programming on freenode
Home Help Search Shop Login Register
Digg This!
Pages: 1 [2]
Print
Author Topic: UDP or TCP/IP?  (Read 4906 times)
AlphaDingDong
Combat Muskrat
All-Around Dev
Hero Member
*

Karma: +32/-3
Offline Offline

Posts: 882
2044.69 points

View Inventory
Send Money to AlphaDingDong
Hey... Are you gonna eat that?


View Profile
« Reply #15 on: September 30, 2009, 10:50:24 AM »

Your listening socket needs to be running already when the sending socket sends.  For something like this I'd recommend setting up a small listener program on your PC.  Otherwise you'll need to create a thread that runs the listener socket and then send the message from the main thread.

Code:
int threadFunction(SceSize argsize, void* arg) {
  //thread body
}

void startThread() {
  int thid = sceKernelCreateThread("Name of thread", threadFunction, 0x10, 0x1000, 0, NULL);
  sceKernelStartThread(thid, 0, NULL);
}

The 'argsize' and 'arg' are whatever you pass in during the call to sceKernelStartThread().  In this case I passed 0 and NULL, but for future reference you can use that to pass info into the thread.

I also see that you're trying to use the same socket for both sending and recieving.  That's not going to work.  Even loopback communication needs to have a sending socket and a recieving socket.  Think of each socket as being like a phone (for TCP) or a fax machine (for UDP dgrams).  Loopback connections are just within the same office, but you still can't call the phone you're calling out from.

Try to start with a program that creates a TCP server socket in a thread and have that thread print out whatever it receives.  Then have the main thread create a socket and connect to the loopback address (127.0.0.1) and send a few lines of text.

Once you have that try direct UDP datagrams with the same architecture and then from there you can try broadcasts.

In any case I don't think it will allow any socket connections whatsoever unless you're connected to an access point.  When I tried to make a loopback test on my PSP it failed to create the socket because I wasn't connected to the router (router here uses WPA2 so I'll have to drag my own one out later).

For the sake of simplicity I usually use Ruby from my PC while I'm testing the PSP code.  Just because Ruby makes it very simple to set up socket stuff.

Here's a server in Ruby that will print out whatever it gets from a TCP connection on port 23.  I can just run that script and then telnet to localhost and whatever I type into telnet shows up in the Ruby console.
Code:
require 'socket' #include socket lib
serv = TCPServer.new(23) #create a server socket on port 23
sock = serv.accept #accept incoming connection (blocking)
sock.write("\n") #send newline so buggy-ass telnet will start displaying characters
while sock.gets #loop until socket read returns nil
  print $_ #the '$_' symbol is the last string fetched from any 'gets' method
end

Ruby can be downloaded for free from http://www.ruby-lang.org and it's pretty easy to set up.  I can write simple test scripts for you and post them here so you don't have to learn the whole language, lol.  It's pretty easy to pick up if you have any programming experience though.

Print messages from UDP connection-based or connectionless datagrams on port 4321:
Code:
require 'socket'

serv = UDPSocket.new
serv.bind(nil, 4321)
loop do
  datagram = serv.recvfrom(64)
  print datagram[0] #datagrams come in 2 part array, part 1 is data part 2 is sender info.
end

« Last Edit: September 30, 2009, 01:48:51 PM by AlphaDingDong » Logged


sels
Newbie
*

Karma: +1/-1
Offline Offline

Posts: 23
2057.08 points

View Inventory
Send Money to sels

View Profile
« Reply #16 on: October 01, 2009, 06:21:45 AM »

Dear AlphaDingDong,
  i'm not very sure exactly on how to make a thread in the PSP. would you mind teaching me how to do it. Sorry for the trouble.
Logged
AlphaDingDong
Combat Muskrat
All-Around Dev
Hero Member
*

Karma: +32/-3
Offline Offline

Posts: 882
2044.69 points

View Inventory
Send Money to AlphaDingDong
Hey... Are you gonna eat that?


View Profile
« Reply #17 on: October 01, 2009, 03:36:30 PM »

Code:
int threadFunction(SceSize argsize, void* arg) {
  //thread body
}

void startThread() {
  int thid = sceKernelCreateThread("Name of thread", threadFunction, 0x10, 0x1000, 0, NULL);
  sceKernelStartThread(thid, 0, NULL);
}

That's all there is to it, really.  The code in the thread body will execute alongside the main thread.  Make sure to put a sceKernelDelay or a wait for vblank inside any loop that doesn't have a blocking call of some sort as the PSP uses delays in a thread as the breakpoint to process other threads.  For instance:

Code:
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>

int running = 1;

int threadFunction(SceSize argsize, void* arg) {
  while(running) {
    pspDebugScreenPrintf("This is the thread talking.\n");
    sceKernelDelayThread(100);
  }
  sceKernelExitThread(0);
  return 0;
}

void startThread() {
  int thid = sceKernelCreateThread("Talking Thread", threadFunction, 0x10, 0x1000, 0, NULL);
  sceKernelStartThread(thid, 0, NULL);
}

int main() {
  pspDebugScreenInit();
  SceCtrlData pad;
  while(running) {
    sceCtrlPeekBufferPositive(&pad, 1);
    if(pad.Buttons & PSP_CTRL_CROSS) {running = 0;}
    pspDebugScreenPrintf("This is the main thread talking.\n");
    sceKernelDelayThread(100);
  }
  sceKernelDelayThread(3000); //pause for a moment so you can see the result
  sceKernelExitGame();
  return 0;
}

That's untested but I think it should work.
« Last Edit: October 01, 2009, 03:39:06 PM by AlphaDingDong » Logged
sels
Newbie
*

Karma: +1/-1
Offline Offline

Posts: 23
2057.08 points

View Inventory
Send Money to sels

View Profile
« Reply #18 on: October 05, 2009, 08:44:09 PM »

dear alphaDingDong,
it just prints out "This is the main thread talking". it is not printing out the other printf. in the talking thread function.
Logged
AlphaDingDong
Combat Muskrat
All-Around Dev
Hero Member
*

Karma: +32/-3
Offline Offline

Posts: 882
2044.69 points

View Inventory
Send Money to AlphaDingDong
Hey... Are you gonna eat that?


View Profile
« Reply #19 on: October 22, 2009, 03:38:27 AM »

Oops.  Forgot to actually start the thread there.

Here...

main.c
Code:
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>

PSP_MODULE_INFO("testthreads", 0, 0, 1);

int running = 1;

int threadFunction(SceSize argsize, void* arg) {
  while(running) {
    pspDebugScreenPrintf("This is the thread talking.\n");
    sceKernelDelayThread(100);
  }
  sceKernelExitThread(0);
  return 0;
}

void startThread() {
  int thid = sceKernelCreateThread("Talking Thread", threadFunction, 0x10, 0x1000, 0, NULL);
  sceKernelStartThread(thid, 0, NULL);
}

int main() {
  pspDebugScreenInit();
  SceCtrlData pad;
  startThread();
  while(running) {
    sceCtrlPeekBufferPositive(&pad, 1);
    if(pad.Buttons & PSP_CTRL_CROSS) {running = 0;}
    pspDebugScreenPrintf("This is the main thread talking.\n");
    sceKernelDelayThread(100);
  }
  sceKernelDelayThread(3000); //pause for a moment so you can see the result
  sceKernelExitGame();
  return 0;
}

makefile
Code:
TARGET = threads
TARGET_FOLDER = K:/PSP/GAME/$(TARGET)
OBJS = main.o
CFLAGS = -O3 -G0 -Wall -Werror
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
LIBDIR =
LIBS =
LDFLAGS =
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Threading Test
PSP_LARGE_MEMORY = 1
PSP_FW_VERSION = 371
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak

eboot:
@if [ -f EBOOT.PBP ]; then echo "Found EBOOT.PBP."; else $(MAKE) --no-print-directory all; fi
@echo "Moving EBOOT to PSP..."
@mv EBOOT.PBP $(TARGET_FOLDER)

update:
@$(MAKE) --no-print-directory eboot
@echo "Done."

install:
@echo "[Installing to PSP]"
@if [ -d $(TARGET_FOLDER) ]; then echo "Found project folder."; else echo "Creating project folder..."; mkdir $(TARGET_FOLDER); fi
@$(MAKE) --no-print-directory eboot
@echo "[Installation complete]"
Logged
sels
Newbie
*

Karma: +1/-1
Offline Offline

Posts: 23
2057.08 points

View Inventory
Send Money to sels

View Profile
« Reply #20 on: October 26, 2009, 12:33:11 AM »

why must i use this ?

Quote
PSP_LARGE_MEMORY = 1
PSP_FW_VERSION = 371
Logged
AlphaDingDong
Combat Muskrat
All-Around Dev
Hero Member
*

Karma: +32/-3
Offline Offline

Posts: 882
2044.69 points

View Inventory
Send Money to AlphaDingDong
Hey... Are you gonna eat that?


View Profile
« Reply #21 on: October 27, 2009, 04:05:34 AM »

It let's the PSP run the program on a slim and also tells it to use the extra RAM of the slim if it is available.
Logged
sels
Newbie
*

Karma: +1/-1
Offline Offline

Posts: 23
2057.08 points

View Inventory
Send Money to sels

View Profile
« Reply #22 on: October 29, 2009, 08:28:38 PM »

Thanks for your help so far AlphaDingDong, one more question, is there any way to input text into the PSP? you know, like the way we input our names when we run a game in the PSP? If possible, is there a function that i must call?
Logged
mowglisanu

C/C++ Developer
Hero Member
*

Karma: +36/-11
Offline Offline

Posts: 787
0.00 points

View Inventory
Send Money to mowglisanu


View Profile
« Reply #23 on: October 29, 2009, 09:12:04 PM »

>is there any way to input text into the PSP? you know, like the way we input our names when we run a game in the PSP?

Yes, that same way

Also there is the debug keyboard

check the samples that came with the sdk(i don't know if there is one with the official osk though but i'm sure you can find examples on the forum)

also the is 9*9's semi-cordal keyboad,
danzoft's(<-spelling) osk. 
I see stinkee2's Easy Graphics lib has an osk
Logged

Check out my:
 Audio lib
 Pmf Viewer
AlphaDingDong
Combat Muskrat
All-Around Dev
Hero Member
*

Karma: +32/-3
Offline Offline

Posts: 882
2044.69 points

View Inventory
Send Money to AlphaDingDong
Hey... Are you gonna eat that?


View Profile
« Reply #24 on: October 30, 2009, 10:01:16 AM »

"Danzeff".

The firmware OSK is nicer but the danzeff OSK can write a bit quicker once you're used to it, provided that it's implemented correctly.  That's a big 'if', though.  I've seen it used rather poorly in a couple places and very smoothly in others. 

There is an OSK sample in the SDK under "sdk/samples/utility/osk".

My OSK code is here: 
Code:
//OSK utility for RMXP/PSP NetPlay
#ifndef __OSK_H__
#define __OSK_H__
//{SciTE Folding - Includes
#include <psputility.h> //Contains main OSK handling
#include <string.h>
//}

extern unsigned list[];

//{SciTE Folding - Functions
void packUIStr(unsigned short* int_str, char* chr_str, int string_size) { //Converts and int string to a char string. (OSK)
  int c;
  memset(chr_str, 0, string_size);
for(c = 0; int_str[c]; c++) {
    chr_str[c] = int_str[c];
  }
}

void expandChrStr(char* chr_str, unsigned short* int_str, int string_size) { //Converts a char string to an int string. (OSK)
  int c;
  memset(int_str, 0, sizeof(unsigned short) * string_size);
for(c = 0; chr_str[c]; c++) {
    int_str[c] = chr_str[c];
  }
}

int getStringFromOSK(int string_size, char* buffer, char* desc, char* intext, int lines) {
  //{SciTE Folding - Create OSK data structure
SceUtilityOskData data; //Declare an OSK data struct (The OSK needs this setting info to run.)
memset(&data, 0, sizeof(data)); //Set all bytes of the struct to 0
  unsigned short int_desc[strlen(desc) + 1]; //Declare an array of unsigned ints to hold our description
  expandChrStr(desc, int_desc, strlen(desc) + 1); //Convert our description to a uint array
data.desc = int_desc; //Pass the description pointer into the struct
unsigned short int_intext[strlen(intext) + 1]; //Same thing, but this time it's the text that will appear in the input box
  expandChrStr(intext, int_intext, strlen(intext) + 1); //As above
data.intext = int_intext; //Text present in input box at start
data.lines = lines; //Number of lines that can be entered (More than 1 adds an 'enter' ("\n") button to the keypad.)
data.outtextlength = string_size; //Entries in outtext array
data.outtextlimit = string_size - 1; //Number of letters that can be entered (It's - 1 to make room for the trailing NULL)
unsigned short outtext[string_size]; //Here we're creating the int string to receive the input data
  memset(outtext, 0, sizeof(unsigned short) * string_size); //And setting its contents to all 0's
data.outtext = outtext; //Then we pass it into the struct, just like the others
data.language = PSP_UTILITY_OSK_LANGUAGE_DEFAULT; //Use the PSP user's chosen language setting
data.unk_24 = 1; //Always set to 1 (Nobody knows...)
  data.inputtype = PSP_UTILITY_OSK_INPUTTYPE_ALL; //Stuff that comes up when you push select.  (See 'psputility_osk.h')
  //}
  //{SciTE Folding - Create OSK parameter structure
SceUtilityOskParams osk; //Create the parameter structure (settings for displaying the OSK)
memset(&osk, 0, sizeof(osk)); //Clear its memory with 0's
osk.base.size = sizeof(osk); //Let it know how big this struct is
sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_LANGUAGE, &osk.base.language); //Set the system message language
sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_UNKNOWN, &osk.base.buttonSwap); //Set the X/O buttonswap based on default
osk.base.graphicsThread = 17; //Thread priority
osk.base.accessThread = 19; //Thread priority
osk.base.fontThread = 18; //Thread priority
osk.base.soundThread = 16; //Thread priority
osk.datacount = 1; //Number of input fields (wth is this?)
osk.data = &data; //Pass it the other struct's location
  //}
  if(sceUtilityOskInitStart(&osk)) {return 0;} //Start the OSK (hopefully)
  int done = 0; //Flag to exit OSK loop
while(!done) { //Process OSK updates until done
    //{SciTE Folding - Clear the screen
sceGuStart(GU_DIRECT,list);
sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
sceGuFinish();
sceGuSync(0,0);
    //}
switch(sceUtilityOskGetStatus()) { //Operate according to status reported by OSK
case PSP_UTILITY_DIALOG_VISIBLE:
sceUtilityOskUpdate(2); //Instruct the OSK to upate
      sceKernelDelayThread(10000); //Slow down to 'normal' speed.  Also allows for any callbacks to run more easily.
break;
case PSP_UTILITY_DIALOG_QUIT:
sceUtilityOskShutdownStart(); //Instruct the OSK to shut down.
break;
case PSP_UTILITY_DIALOG_NONE:
done = 1; //Break the loop
break;
}
sceDisplayWaitVblankStart();
flipScreen();
}
  //int_str for output is now stored in 'outtext' (same pointer value as data.outtext).
  packUIStr(outtext, buffer, string_size); //And now it's packed into a proper char string in our 'return' buffer.
  clearScreen(0);
  return 1; //Success!  :)
}
//}
#endif

It looks very messy in the box there but if you paste it into an editor it should be more clear.
That's intended to be added as a to a project as a seperate file.  Then you'd just call

int getStringFromOSK(int string_size, char* buffer, char* desc, char* intext, int lines)

string_size - number of characters in char buffer
buffer - char buffer to fill
desc - description text to appear when OSK runs
intext - text already in the text box when OSK runs
lines - maximum number of lines the OSK is allowed to return

Function returns 0 on failure and 1 on success.

Like the net dialog this requires that the GU is already set up and running.

I'd recommend not using it 'as-is', but rather reading through the code and comments and then writing your own implementation.
« Last Edit: October 30, 2009, 10:04:08 AM by AlphaDingDong » Logged
sels
Newbie
*

Karma: +1/-1
Offline Offline

Posts: 23
2057.08 points

View Inventory
Send Money to sels

View Profile
« Reply #25 on: November 13, 2009, 12:03:03 AM »

Is there a way to clear the screen after i flipscreen a picture to the PSP?
Logged
Pages: 1 [2]
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.751 seconds with 34 queries.
Sister Sites: Guitar Hero 4   BrokeniTouch.com