Skip to content

Commit

Permalink
Rework networking sample to TCP server #77
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryptogenic committed Aug 8, 2020
1 parent e2e028d commit 2695279
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 60 deletions.
2 changes: 1 addition & 1 deletion samples/networking/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PROJDIR := $(shell basename $(CURDIR))
INTDIR := $(PROJDIR)/x64/Debug

# Libraries linked into the ELF.
LIBS := -lc -lkernel -lScePad -lSceUserService
LIBS := -lc -lkernel -lc++

# Compiler options. You likely won't need to touch these.
UNAME_S := $(shell uname -s)
Expand Down
2 changes: 1 addition & 1 deletion samples/networking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Version](https://img.shields.io/badge/Version-1.0-brightgreen.svg)](https://github.com/Cryptogenic/OpenOrbis-PS4-Toolchain)

This project contains example code for a basic TCP client that connects to a given IP address, sends a ping packet, and closes the connection.
This project contains example code for a basic TCP server that runs on `0.0.0.0:9025` and sends "Hello" to any client that connects to it before terminating the connection.



Expand Down
2 changes: 1 addition & 1 deletion samples/networking/networking/build.bat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SETLOCAL EnableDelayedExpansion

Rem Libraries to link in
set libraries=-lc -lkernel -lc++ -lScePad -lSceUserService
set libraries=-lc -lkernel -lc++

Rem Read the script arguments into local vars
set intdir=%1
Expand Down
107 changes: 50 additions & 57 deletions samples/networking/networking/main.cpp
Original file line number Diff line number Diff line change
@@ -1,86 +1,79 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <orbis/libkernel.h>
#include <orbis/Pad.h>
#include <orbis/UserService.h>

#define IP "192.168.0.10"
#define PORT 9030
#include "../../_common/log.h"

void waitOnButton(int pad, unsigned int button)
{
OrbisPadData padData;

for(;;)
{
scePadReadState(pad, &padData);

if(padData.buttons & button) {
return;
}
}
}
#define PORT 9025

// Logging
std::stringstream debugLogStream;

int main(void)
{
int userID;
const char *msg = "ping";
struct sockaddr_in addr;
int sockfd;
int connfd;
socklen_t addrLen;

struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;

// No buffering
setvbuf(stdout, NULL, _IONBF, 0);

// Create a server socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);

// Initialize the Pad library
if (scePadInit() != 0)
if (sockfd < 0)
{
printf("[DEBUG] [ERROR] Failed to initialize pad library!\n");
return -1;
DEBUGLOG << "Failed to create socket: " << strerror(errno);
for (;;);
}

// Get the user ID
OrbisUserServiceInitializeParams param;
param.priority = ORBIS_KERNEL_PRIO_FIFO_LOWEST;
sceUserServiceInitialize(&param);
sceUserServiceGetInitialUser(&userID);

// Open a handle for the controller
int pad = scePadOpen(userID, 0, 0, NULL);
// Bind to 0.0.0.0:PORT
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(PORT);

if (pad < 0)
if (bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) != 0)
{
printf("[DEBUG] Failed to open pad!\n");
return -1;
DEBUGLOG << "Failed to bind to 0.0.0.0:" << PORT << ": " << strerror(errno);
for (;;);
}

// Setup the socket
(void)memset(&addr, 0, sizeof(struct sockaddr_in));

int sock = socket(AF_INET, SOCK_STREAM, 0);
// Listen and accept clients
addrLen = sizeof(clientAddr);

addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);

// Wait on X
waitOnButton(pad, ORBIS_PAD_BUTTON_CROSS);

if (inet_pton(AF_INET, IP, &addr.sin_addr) <= 0)
if (listen(sockfd, 5) != 0)
{
printf("[DEBUG] [ERROR] IP Address not supported.\n");
return -1;
DEBUGLOG << "Failed to listen: " << strerror(errno);
for (;;);
}

if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
for (;;)
{
printf("[DEBUG] [ERROR] Failed to connect to %s:%d.\n", IP, PORT);
return -1;
}
connfd = accept(sockfd, (struct sockaddr*)&clientAddr, &addrLen);

if (connfd < 0)
{
DEBUGLOG << "Failed to accept client: " << strerror(errno);
for (;;);
}

printf("[DEBUG] Sending message to %s:%d.\n", IP, PORT);
send(sock, msg, strlen(msg), 0);
printf("[DEBUG] Message sent. Closing and infinitely looping.\n");
DEBUGLOG << "Accepted client '" << connfd << "'";

close(sock);
for (;;) {}
// Write a "hello" message then terminate the connection
const char msg[] = "hello\n";
write(connfd, msg, sizeof(msg));
close(connfd);

DEBUGLOG << "Closed client '" << connfd << "'";
}
}

0 comments on commit 2695279

Please sign in to comment.