So I added in my own commands and allowed the server to play music\sounds, but what good is that if I'm away from the server? I also followed another guide to get the IP to appear in the x.x.x.x format as opposed to the hex format which was made for winsock, but still works all the same.
Anyway, we come to the problem that I'm trying to figure out is how can I play music\sounds from the server and play them on the client? (Streaming) I assume I need a buffer or some sort to send a little bit at a time, and free the memory once it's done, but to be perfectly honest, I have no idea how.
Anyhow, here's my current server code (Since I'm new to this, I left the comments from the SDL_Net Wiki page there):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <SDL_mixer.h>
#include <SDL_net.h>
Mix_Music* alien = NULL;
TCPsocket sd, csd; /* Socket descriptor, Client socket descriptor */
IPaddress ip, *remoteIP;
void Clean()
{
SDLNet_TCP_Close(sd);
SDLNet_Quit();
if (Mix_PlayingMusic() == 1)
Mix_HaltMusic();
Mix_FreeMusic(alien);
Mix_CloseAudio();
}
int main(int argc, char **argv)
{
if( Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) < 0)
{
FILE* lerr = fopen("errlog.txt", "wb+");
fprintf(lerr, "Error opening audio.\n");
fclose(lerr);
exit(EXIT_FAILURE);
}
atexit(Clean);
alien = Mix_LoadMUS("alien.mp3");
if (!alien)
{
FILE* lerr = fopen("errlog.txt", "wb+");
fprintf(lerr, "Error opening alien.mp3.\n");
fclose(lerr);
exit(EXIT_FAILURE);
}
int quit, quit2;
char buffer[512];
if (SDLNet_Init() < 0)
{
FILE* lerr = fopen("errlog.txt", "wb+");
fprintf(lerr, "SDLNet_Init: %s\n", SDLNet_GetError());
fclose(lerr);
exit(EXIT_FAILURE);
}
/* Resolving the host using NULL make network interface to listen */
if (SDLNet_ResolveHost(&ip, NULL, 2000) < 0)
{
FILE* lerr = fopen("errlog.txt", "wb+");
fprintf(lerr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
fclose(lerr);
exit(EXIT_FAILURE);
}
/* Open a connection with the IP provided (listen on the host's port) */
if (!(sd = SDLNet_TCP_Open(&ip)))
{
FILE* lerr = fopen("errlog.txt", "wb+");
fprintf(lerr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
fclose(lerr);
exit(EXIT_FAILURE);
}
/* Wait for a connection, send data and term */
quit = 0;
while (!quit)
{
/* This check the sd if there is a pending connection.
* If there is one, accept that, and open a new socket for communicating */
if ((csd = SDLNet_TCP_Accept(sd)))
{
/* Now we can communicate with the client using csd socket
* sd will remain opened waiting other connections */
/* Get the remote address */
if ((remoteIP = SDLNet_TCP_GetPeerAddress(csd)))
{
/* Print the address, converting in the host format */
unsigned char IP[4] = {0,0,0,0};
for (int i=0; i<4; i++)
{
IP = ( SDLNet_Read32(&remoteIP->host) >> (i*8) ) & 0xFF;
}
printf("Client connected: %d.%d.%d.%d:%d\n", IP[3],IP[2],IP[1],IP[0], SDLNet_Read16(&remoteIP->port));
}
else
{
FILE* lerr = fopen("errlog.txt", "wb+");
fprintf(lerr, "SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());
fclose(lerr);
}
quit2 = 0;
while (!quit2)
{
if (SDLNet_TCP_Recv(csd, buffer, 512) > 0)
{
printf("Client said: %s\n", buffer);
if(strcmp(buffer, "exit") == 0) /* Terminate this connection */
{
quit2 = 1;
printf("Client terminated the connection...\n");
}
if(strcmp(buffer, "quit") == 0) /* Quit the program */
{
quit2 = 1;
quit = 1;
printf("Client terminated server...\n");
}
if (strcmp(buffer, "alien") == 0)
{
if (Mix_PlayingMusic() == 0)
Mix_PlayMusic(alien, -1);
}
if (strcmp(buffer, "stop") == 0)
{
if (Mix_PlayingMusic() == 1)
Mix_HaltMusic();
}
if (strcmp(buffer, "pause") == 0)
{
if (Mix_PlayingMusic() == 1)
Mix_PauseMusic();
}
if (strcmp(buffer, "resume") == 0)
{
if (Mix_PausedMusic() == 1)
Mix_ResumeMusic();
}
}
}
/* Close the client socket */
SDLNet_TCP_Close(csd);
}
}
return EXIT_SUCCESS;
}
And here is my client code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL_net.h>
int main(int argc, char **argv)
{
IPaddress ip; /* Server address */
TCPsocket sd; /* Socket descriptor */
int quit, len;
char buffer[512];
/* Simple parameter checking */
if (argc < 3)
{
FILE* lerr = fopen("lerr.txt", "w+");
fprintf(lerr, "Usage: %s host port\n", argv[0]);
fclose(lerr);
exit(EXIT_FAILURE);
}
if (SDLNet_Init() < 0)
{
FILE* lerr = fopen("lerr.txt", "w+");
fprintf(lerr, "SDLNet_Init: %s\n", SDLNet_GetError());
fclose(lerr);
exit(EXIT_FAILURE);
}
/* Resolve the host we are connecting to */
if (SDLNet_ResolveHost(&ip, argv[1], atoi(argv[2])) < 0)
{
FILE* lerr = fopen("lerr.txt", "w+");
fprintf(lerr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
fclose(lerr);
exit(EXIT_FAILURE);
}
/* Open a connection with the IP provided (listen on the host's port) */
if (!(sd = SDLNet_TCP_Open(&ip)))
{
FILE* lerr = fopen("lerr.txt", "w+");
fprintf(lerr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
fclose(lerr);
exit(EXIT_FAILURE);
}
/* Send messages */
quit = 0;
while (!quit)
{
printf(">");
scanf("%s", buffer);
len = strlen(buffer) + 1;
if (SDLNet_TCP_Send(sd, (void *)buffer, len) < len)
{
FILE* lerr = fopen("lerr.txt", "w+");
fprintf(lerr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
fclose(lerr);
exit(EXIT_FAILURE);
}
if(strcmp(buffer, "exit") == 0)
quit = 1;
if(strcmp(buffer, "quit") == 0)
quit = 1;
}
SDLNet_TCP_Close(sd);
SDLNet_Quit();
return EXIT_SUCCESS;
}
Any help is appreciated