[Cruce-commit] SF.net SVN: cruce:[17] C/trunk
Status: Beta
Brought to you by:
caiusb
|
From: <ca...@us...> - 2010-03-10 11:26:48
|
Revision: 17
http://cruce.svn.sourceforge.net/cruce/?rev=17&view=rev
Author: caiusb
Date: 2010-03-10 11:26:41 +0000 (Wed, 10 Mar 2010)
Log Message:
-----------
Am creat ierarhia de directoare si am pus Makefile-uri in pentru server si
client.
Added Paths:
-----------
C/trunk/client/
C/trunk/client/Makefile
C/trunk/client/client.c
C/trunk/client/client_lavinia.c
C/trunk/protocol/
C/trunk/protocol/protocol.c
C/trunk/protocol/protocol.h
Removed Paths:
-------------
C/trunk/.project
C/trunk/Server/
C/trunk/client.c
C/trunk/protocol.h
C/trunk/server.c
C/trunk/server.h
C/trunk/src/
Deleted: C/trunk/.project
===================================================================
--- C/trunk/.project 2010-03-09 22:18:55 UTC (rev 16)
+++ C/trunk/.project 2010-03-10 11:26:41 UTC (rev 17)
@@ -1,81 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
- <name>Cruce</name>
- <comment></comment>
- <projects>
- </projects>
- <buildSpec>
- <buildCommand>
- <name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
- <triggers>clean,full,incremental,</triggers>
- <arguments>
- <dictionary>
- <key>?name?</key>
- <value></value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.append_environment</key>
- <value>true</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.autoBuildTarget</key>
- <value>all</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.buildArguments</key>
- <value></value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.buildCommand</key>
- <value>make</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.buildLocation</key>
- <value>${workspace_loc:/Cruce/Debug}</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.cleanBuildTarget</key>
- <value>clean</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.contents</key>
- <value>org.eclipse.cdt.make.core.activeConfigSettings</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.enableAutoBuild</key>
- <value>false</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.enableCleanBuild</key>
- <value>true</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.enableFullBuild</key>
- <value>true</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.fullBuildTarget</key>
- <value>all</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.stopOnError</key>
- <value>true</value>
- </dictionary>
- <dictionary>
- <key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
- <value>true</value>
- </dictionary>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.eclipse.cdt.core.cnature</nature>
- <nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
- <nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
- </natures>
-</projectDescription>
Added: C/trunk/client/Makefile
===================================================================
--- C/trunk/client/Makefile (rev 0)
+++ C/trunk/client/Makefile 2010-03-10 11:26:41 UTC (rev 17)
@@ -0,0 +1,14 @@
+objects = client.o protocol.o
+
+client: $(objects)
+ cc -o client $(objects)
+
+client.o: client.c ../protocol/protocol.h
+ cc -c client.c
+
+protocol.o: ../protocol/protocol.h ../protocol/protocol.c
+ cc -c ../protocol/protocol.c
+
+clean:
+ rm -f $(objects)
+ rm -f client
Added: C/trunk/client/client.c
===================================================================
--- C/trunk/client/client.c (rev 0)
+++ C/trunk/client/client.c 2010-03-10 11:26:41 UTC (rev 17)
@@ -0,0 +1,110 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+/*
+* aici partea nashpa e ca clientul depinde de server.
+* NOT OK! Clientul si serverul depind numai de protocol
+* si nu intre ele
+*/
+#include "../server/server.h"
+#include "../protocol/protocol.h"
+
+struct jucator player;
+
+int join_game() {
+ char message[1024];
+ char answer = 'n';
+ while (answer != 'y') {
+ printf("Join game? [y|n]");
+ scanf("%c%*c", &answer);
+ }
+ memset(message, 0, 1024);
+ sprintf(message, "%d %s", JOIN_GAME, player.nume);
+ write(player.sfd, &message, strlen(message));
+
+ memset(message, 0, 1024);
+ read(player.sfd, &message, 1024);
+ int code = decode_message(message);
+ printf("Received %s from server\n", message);
+ if (code == NACK)
+ return 0;
+ else if (code == ACK)
+ return 1;
+}
+
+int start_game() {
+ char message[1024];
+ char answer = 'n';
+ while (answer != 'y') {
+ printf("Start game? [y|n]");
+ scanf("%c%*c", &answer);
+ }
+
+ memset(message, 0, 1024);
+ sprintf(message, "%d", START_GAME);
+ write(player.sfd, &message, strlen(message));
+
+ memset(message, 0, 1024);
+ read(player.sfd, &message, 1024);
+ int code = decode_message(message);
+ printf("Received %s from server\n", message);
+ if (code == NACK)
+ return 0;
+ else if (code == ACK)
+ return 1;
+}
+
+/** adresa, port, nume **/
+int main(int argc, char *argv[]) {
+ if (argc != 4) {
+ printf("eroare la nr de argumente\n");
+ exit(1);
+ }
+
+ char ip[16]; //adresa IP a serverului
+ strcpy(ip, argv[1]);
+ int port = atoi(argv[2]); //portul serverului
+ strcpy(player.nume, argv[3]);
+
+ player.sfd = socket(PF_INET, SOCK_STREAM, 0);
+ if (player.sfd == -1) {
+ printf("eroare la crearea socketului\n");
+ exit(1);
+ }
+
+ struct sockaddr_in serverSocket;
+ memset(&serverSocket, 0, sizeof(struct sockaddr_in));
+ serverSocket.sin_family = AF_INET;
+ serverSocket.sin_port = htons(port);
+ if (inet_aton(ip, &serverSocket.sin_addr) == 0) {
+ printf("eroare la adresa ip\n");
+ exit(1);
+ }
+
+ if (connect(player.sfd, (struct sockaddr *) &serverSocket, sizeof(struct sockaddr_in)) == -1) {
+ printf("eroare la crearea conexinii\n");
+ close(player.sfd);
+ exit(1);
+ }
+
+ printf("Connected to server\n");
+
+ if (!join_game()) {
+ printf("couldn't join the game\n");
+ exit(1);
+ }
+ //wait for 4 messages about players
+ if(!start_game()) {
+ printf("couldn't start the game\n");
+ exit(1);
+ }
+ printf("Now we play. Next time.\n");
+
+ shutdown(player.sfd, SHUT_RDWR);
+ close(player.sfd);
+ return 0;
+}
Added: C/trunk/client/client_lavinia.c
===================================================================
--- C/trunk/client/client_lavinia.c (rev 0)
+++ C/trunk/client/client_lavinia.c 2010-03-10 11:26:41 UTC (rev 17)
@@ -0,0 +1,97 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+int main(int argc, char *argv[])
+{
+ int sfd;
+ struct sockaddr_in addr;
+ char buf, ip[16], buf1[100], *name;
+ int port, n=0;
+
+ if (argc == 1)
+ {
+ strcpy(ip,"127.0.0.1");
+ port = 1100;
+ }
+ else
+ if (argc == 2)
+ {
+ printf("Usage %s [ip port]\n",argv[0]);
+ exit(1);
+ }
+ else
+ {
+ strcpy(ip,argv[1]);
+ port = atoi(argv[2]);
+ }
+
+ // creare socket
+ sfd = socket(PF_INET, SOCK_STREAM, 0);
+ if (sfd == -1)
+ {
+ perror("Error creating socket");
+ exit(1);
+ }
+
+ memset(&addr,0,sizeof(struct sockaddr_in));
+ addr.sin_family = AF_INET;
+
+ // transformare IP din Internet Standard dot notation in Network standard
+ if (inet_aton(ip,&addr.sin_addr) == -1)
+ {
+ perror("Invalid address");
+ exit(1);
+ }
+
+ // transformare adresa port in network byte order/ short (2 octeti)
+ addr.sin_port = htons(port);
+
+ // conectare la server
+ if (connect(sfd,(struct sockaddr *) &addr, sizeof(struct sockaddr_in)) == -1)
+ {
+ perror("Error connecting to server");
+ exit(1);
+ }
+
+ printf("Connected to server\n");
+
+ // JOIN THE GAME
+ //---citire de la tastatura a numelui
+ printf("Do you wish to join the game. If so, please type your name");
+ scanf("%s",name);
+
+ //---construire mesaj de join
+ strcpy(buf,"1 ");
+ strcat(buf,name);
+ strcat(buf," \n");
+
+ //---trimitere catre server
+ write(sfd,&buf,strlen(buf));
+
+ //---MESSAGE FROM THE SERVER
+ memset(&buf1,0,100);
+ while (read(sfd,&buf,1))
+ buf1[n++] = buf;
+ strcat(buf1,"\n");
+ printf("%s",buf1);
+
+ // START THE GAME
+ printf("Do you wish to start the game? If so, please press any key!")
+ while(read(0,&buf,1) == 0)
+ ;
+ strcpy(buf,"2 \n");
+ write(sfd,&buf,strlen(buf));
+
+ //---MESSAGE FROM THE SERVER
+ memset(&buf1,0,100);
+ n = 0;
+ while (read(sfd,&buf,1))
+ buf1[n++] = buf;
+ strcat(buf1,"\n");
+ printf("%s",buf1);
+
+ close(sfd);
+}
Deleted: C/trunk/client.c
===================================================================
--- C/trunk/client.c 2010-03-09 22:18:55 UTC (rev 16)
+++ C/trunk/client.c 2010-03-10 11:26:41 UTC (rev 17)
@@ -1,97 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-int main(int argc, char *argv[])
-{
- int sfd;
- struct sockaddr_in addr;
- char buf, ip[16], buf1[100], *name;
- int port, n=0;
-
- if (argc == 1)
- {
- strcpy(ip,"127.0.0.1");
- port = 1100;
- }
- else
- if (argc == 2)
- {
- printf("Usage %s [ip port]\n",argv[0]);
- exit(1);
- }
- else
- {
- strcpy(ip,argv[1]);
- port = atoi(argv[2]);
- }
-
- // creare socket
- sfd = socket(PF_INET, SOCK_STREAM, 0);
- if (sfd == -1)
- {
- perror("Error creating socket");
- exit(1);
- }
-
- memset(&addr,0,sizeof(struct sockaddr_in));
- addr.sin_family = AF_INET;
-
- // transformare IP din Internet Standard dot notation in Network standard
- if (inet_aton(ip,&addr.sin_addr) == -1)
- {
- perror("Invalid address");
- exit(1);
- }
-
- // transformare adresa port in network byte order/ short (2 octeti)
- addr.sin_port = htons(port);
-
- // conectare la server
- if (connect(sfd,(struct sockaddr *) &addr, sizeof(struct sockaddr_in)) == -1)
- {
- perror("Error connecting to server");
- exit(1);
- }
-
- printf("Connected to server\n");
-
- // JOIN THE GAME
- //---citire de la tastatura a numelui
- printf("Do you wish to join the game. If so, please type your name");
- scanf("%s",name);
-
- //---construire mesaj de join
- strcpy(buf,"1 ");
- strcat(buf,name);
- strcat(buf," \n");
-
- //---trimitere catre server
- write(sfd,&buf,strlen(buf));
-
- //---MESSAGE FROM THE SERVER
- memset(&buf1,0,100);
- while (read(sfd,&buf,1))
- buf1[n++] = buf;
- strcat(buf1,"\n");
- printf("%s",buf1);
-
- // START THE GAME
- printf("Do you wish to start the game? If so, please press any key!")
- while(read(0,&buf,1) == 0)
- ;
- strcpy(buf,"2 \n");
- write(sfd,&buf,strlen(buf));
-
- //---MESSAGE FROM THE SERVER
- memset(&buf1,0,100);
- n = 0;
- while (read(sfd,&buf,1))
- buf1[n++] = buf;
- strcat(buf1,"\n");
- printf("%s",buf1);
-
- close(sfd);
-}
Added: C/trunk/protocol/protocol.c
===================================================================
--- C/trunk/protocol/protocol.c (rev 0)
+++ C/trunk/protocol/protocol.c 2010-03-10 11:26:41 UTC (rev 17)
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "protocol.h"
+
+int decode_message(char *message) {
+ int code;
+
+ if (message[1] <= '9' && message[1] >= '0') {
+ code = atoi(message);
+ strcpy(message, message + 3);
+ } else {
+ code = atoi(message);
+ strcpy(message, message + 2);
+ }
+ /*
+ printf("Decoded this message:\n");
+ printf("code: %d\n", code);
+ printf("message: %s\n", message);
+ printf("---end---\n");
+ */
+ return code;
+}
+
+void pad(char *msg) {
+ int size = strlen(msg);
+ int i;
+ for (i=size;i<MSG_SIZE;i++)
+ msg[i] = PAD_CHAR;
+ msg[MSG_SIZE-1] = '\0';
+}
Added: C/trunk/protocol/protocol.h
===================================================================
--- C/trunk/protocol/protocol.h (rev 0)
+++ C/trunk/protocol/protocol.h 2010-03-10 11:26:41 UTC (rev 17)
@@ -0,0 +1,24 @@
+#ifndef __PROTOCOL_H__
+#define __PROTOCOL_H__
+
+#define MSG_SIZE 1024
+#define PAD_CHAR ' '
+
+#define JOIN_GAME 1
+#define ACK 2
+#define NACK 3
+#define INFO_MSG 4
+#define START_GAME 5
+#define SEND_CARDS 6
+#define BEGIN_BID 7
+#define BID_? 8
+#define BID 9
+#define HIT 10
+#define CARD_ANUNT 11
+
+#define DELIMITER " "
+
+int decode_message(char *message);
+void pad(char *msg);
+
+#endif /* __PROTOCOL_H__ */
Deleted: C/trunk/protocol.h
===================================================================
--- C/trunk/protocol.h 2010-03-09 22:18:55 UTC (rev 16)
+++ C/trunk/protocol.h 2010-03-10 11:26:41 UTC (rev 17)
@@ -1,18 +0,0 @@
-#ifndef __PROTOCOL_H__
-#define __PROTOCOL_H__
-
-#define JOIN_GAME 1
-#define ACK 2
-#define NACK 3
-#define INFO_MSG 4
-#define START_GAME 5
-#define SEND_CARDS 6
-#define BEGIN_BID 7
-#define BID_? 8
-#define BID 9
-#define HIT 10
-#define CARD_ANUNT 11
-
-int decode_message(char *message);
-
-#endif /* __PROTOCOL_H__ */
Deleted: C/trunk/server.c
===================================================================
--- C/trunk/server.c 2010-03-09 22:18:55 UTC (rev 16)
+++ C/trunk/server.c 2010-03-10 11:26:41 UTC (rev 17)
@@ -1,219 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-//Our libraries
-#include "server.h"
-#include "protocol.h"
-
-
-#define NUMAR_ECHIPE 2
-
-
- //Server related fields
-struct echipa echipe[NUMAR_ECHIPE];
-struct sockaddr_in serverSocketAddress;
-char serverIP[16];
-int serverPort;
-int server_sfd;
-char messageDelimiter[] = " ";
-FILE *outputInfo;// = stdout;
-FILE *outputError;// = stdout;
-
-void decodeMessage(char message[], struct jucator sender)
-{
-
- if(message == NULL)
- {
- informationMessage(outputError, "The message is NULL!");
- exit(1);
- }
-
- informationMessage(outputInfo, "Starting message decoding");
-
- //Starting to decode the message
- char *header = strtok(message, messageDelimiter);
-
- informationMessage(outputInfo, "header = %s" , header);
-
- if(header == NULL)
- {
- informationMessage(outputError,"Error at separating the message header!\n");
- exit(0);
- }
-
- char *rest = strtok(NULL, messageDelimiter);
-
- int messageType = atoi(header);
-
- if(messageType == 0)
- {
- informationMessage(outputError, "Error decoding the message header!\n");
- exit(0);
- }
- informationMessage(outputInfo, "Successful message header separation.\n");
-
- switch(messageType)
- {
- case JOIN_GAME:
- {
- int i;
- int j;
- for (i = 0; i < NUMAR_ECHIPE; i++)
- {
- if(echipe[i].jucatori_activi < JUCATORI_PER_ECHIPA)
- {
- echipe[i].jucatori[echipe[i].jucatori_activi++] = sender;
- }
- }
-
- if(i == NUMAR_ECHIPE)
- {
- sendNACKMessage(sender, "Nu mai sunt locuri!\n");
- }
-
- //sending an acknowledge message
- sendAckMessage(sender, "Cerere acceptata.\n");
-
-
- for (i = 0; i < NUMAR_ECHIPE; i++)
- {
- for (j = 0; j < echipe[i].jucatori_activi; j++)
- {
- //To sent information message to the rest of players
- }
- }
- break;
- }
- }
-
-}
-
-void sendAckMessage(struct jucator receiver, char message[])
-{
- //adding the header
- char buffer[100];
- sprintf(buffer, "%d %s", ACK, message);
-
- write(receiver.sfd, &message, strlen(message) * sizeof(char));
-}
-
-void sendNACKMessage(struct jucator receiver, char message[])
-{
- //adding header
- //adding the header
- char buffer[100];
- sprintf(buffer, "%d %s", NACK, message);
-
- write(receiver.sfd, &message, strlen(message) * sizeof(char));
-}
-
-void informationMessage(FILE *output, char message[])
-{
- fprintf(output, "%s", message);
-}
-
-
-void setUpServer(char ip[], int port)
-{
- printf("Starting to set up the server.\n");
- strcpy(serverIP, ip);
- serverPort = port;
-
- //Setting up the sever socket
- server_sfd = socket(PF_INET, SOCK_STREAM, 0);
- if (server_sfd == -1)
- {
- perror("Error creating Server socket!\n");
- exit(1);
- }
- memset(&serverSocketAddress, 0, sizeof(struct sockaddr_in));
- serverSocketAddress.sin_family = AF_INET;
- if (inet_aton(serverIP, &serverSocketAddress.sin_addr) == 0)
- {
- perror("Address not valid!\n");
- exit(1);
- }
- serverSocketAddress.sin_port = htons(serverPort);
-
- if (bind(server_sfd, (struct server *) &serverSocketAddress, sizeof(struct sockaddr_in)) == -1)
- {
- perror("Error binding to address");
- exit(1);
- }
-
- printf("Server set up to IP address: %s and binded to port %d.\n", serverIP, serverPort);
-}
-
-void startListening(int server_sfd)
-{
- if (listen(server_sfd, 100) == -1)
- {
- perror("Error listening to socket");
- exit(1);
- }
- else
- {
- printf("Server starting to listen.\n");
- }
-}
-
-int acceptConnection(int socketFileDescriptor)
-{
- informationMessage(outputInfo, "Accepting a connection request");
- struct sockaddr_in in_conn;
- socklen_t socksize = sizeof(struct sockaddr_in);
-
- memset(&in_conn, 0, sizeof(struct sockaddr_in));
-
- int connFileDescriptor = accept(socketFileDescriptor, (struct sockaddr *) &in_conn, &socksize);
-
- if (connFileDescriptor == -1)
- {
- informationMessage(outputError, "Error accepting connection");
- exit(1);
- }
-
- /*
- char *c = sprintf("Received connection from: %s\n", inet_ntoa(in_conn.sin_addr.s_addr));
- informationMessage(outputInfo, c);*/
-
- return connFileDescriptor;
-}
-
-void cleanUp()
-{
- close(server_sfd);
-}
-
-int main()
-{
- outputInfo = stdout;
- outputError = stdout;
-
-
- setUpServer("127.0.0.1", 1100);
-
- startListening(server_sfd);
-
- int playersConnected = 0;
- while(playersConnected < MAX_JUCATORI)
- {
- //Accepting connections from players
- int playerFileDescriptor = acceptConnection(server_sfd);
-
- echipe[playersConnected / NUMAR_ECHIPE].jucatori[playersConnected % JUCATORI_PER_ECHIPA].sfd = playerFileDescriptor;
- char c[100];
- sprintf(c, "%d players connected.\n", playersConnected);
- informationMessage(outputInfo, c);
- playersConnected++;
- }
-
- informationMessage(outputInfo, "All players connected to the server.\n");
-
- close(server_sfd);
-
- return 0;
-}
Deleted: C/trunk/server.h
===================================================================
--- C/trunk/server.h 2010-03-09 22:18:55 UTC (rev 16)
+++ C/trunk/server.h 2010-03-10 11:26:41 UTC (rev 17)
@@ -1,40 +0,0 @@
-#ifndef __SERVER_H__
-#define __SERVER_H__
-
-#define MAX_JUCATORI 4
-#define MAX_CARTI 24
-#define CARTI_MANA 6
-#define JUCATORI_PER_ECHIPA 2
-
-/**
-Structura pentru o carte
-valoare - valoare cartii (0,2,3,4,10,11)
-culoare - culoarea (D,V,M,R - Duba, Verde, Mac si Rosu).
-**/
-struct carte {
- int valoare;
- char culoare;
-};
-
-/**
-Structura pentru un jucator
-**/
-struct jucator {
- struct carte carti[CARTI_MANA];
- int carti_ramase;
- char nume[20];
- int sfd; /* socket file descriptor */
-};
-
-/**
-Structura pentru o echipa de 2 jucatori
-**/
-struct echipa {
- struct jucator jucatori[JUCATORI_PER_ECHIPA];
- int jucatori_activi;
- int anunturi;
- int scor;
- struct carte carti_luate[MAX_CARTI];
-};
-
-#endif /* __SERVER_H__ */
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|