Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- SERVER THINGS
- -------------
- a place to put various things that either need to be done or explained
- TO DO:
- ------
- 1.Write an interpreter for my tcp protocol
- 2.Make it do things based on the results of the interpreter
- 3.Open VS and give this program a cute icon
- NOTES:
- -----
- do the woom
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #pragma comment(lib, "Ws2_32.lib")
- int main() {
- WSADATA wsa;
- int temp_result, server_size;
- SOCKET s, clientsock;
- struct sockaddr_in server, client;
- temp_result = WSAStartup(MAKEWORD(2,2), &wsa);
- if (temp_result != 0) {
- printf("aaa wsa died - %i", WSAGetLastError()); //help pls :c
- return 1;
- };
- if ((s = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR) {
- printf("socket is rip - %i", WSAGetLastError()); //save me
- return 2;
- };
- server.sin_addr.s_addr = INADDR_ANY;
- server.sin_family = AF_INET;
- server.sin_port = htons(2127);
- if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) {
- printf("mome help me - %i", WSAGetLastError()); //asdaw
- return 3;
- };
- listen(s,3);
- server_size = sizeof(server); //kind of ugly variable made out of necessity
- int filesending = 0;
- int filesize = 0;
- int bytessent = 0;
- char *rawfile;
- while ((clientsock = accept(s, (struct sockaddr *)&client, &server_size)) != INVALID_SOCKET) {
- char stream[2048];
- temp_result = recv(clientsock, stream, 2048, 0);
- if (temp_result < 1) {
- printf("help game is dead %i %i", temp_result, WSAGetLastError());
- return 4;
- };
- //first figure out what the fuck it is
- if (stream[0] == '!') {
- filesending = 1;
- //first find length of the total file
- int length = 0;
- for(int i = 0; i < 4; i++) {
- int num_part = 0;
- num_part = (int)stream[i + 1];
- num_part = num_part << 8*(3-i);
- length = length | num_part;
- };
- filesize = length;
- printf("%i", length);
- rawfile = (char*) realloc(rawfile, length);
- };
- temp_result = send(clientsock,(const char *)stream, 2048, 0);
- if (temp_result == SOCKET_ERROR) {
- printf("help i need somebody help not just anybody help i need someone, help %i", WSAGetLastError()); //damn it dad this is your fault
- return 5;
- };
- };
- closesocket(s);
- WSACleanup();
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement