Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <pthread.h>
- int main() {
- int choice;
- printf("Welcome to slack! \n \n");
- //printf("Please write your nickname: "); scanf("%s",nick); system("CLS");
- printf("Type 1 for CHAT, 2 for FILE MANAGER: \n"); scanf("%d",&choice);
- while(choice != 1 && choice != 2)
- {
- printf("Somthing went wrong, please try again: "); scanf("%d",&choice);
- }
- if(choice == 1)
- {
- struct sockaddr_in server_addr;
- client = socket(AF_INET, SOCK_STREAM, 0);
- /* ---------- ESTABLISHING SOCKET CONNECTION ----------*/
- /* --------------- socket() function ------------------*/
- if (client < 0)
- {
- cout << "\nError establishing socket..." << endl;
- exit(1);
- }
- cout << "\n=> Socket client has been created..." << endl;
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(portNum);
- if (connect(client,(struct sockaddr *)&server_addr, sizeof(server_addr)) == 0)
- cout << "=> Connection to the server port number: " << portNum << endl;
- cout << "=> Awaiting confirmation from the server..." << endl;
- recv(client, buffer, bufsize, 0);
- cout << "=> Connection confirmed, you are good to go...";
- cout << "\n\n=> Enter # to end the connection\n" << endl;
- do {
- cout << "tu powinien byc klient: " << client;
- do {
- cin >> buffer;
- send(client, buffer, bufsize, 0);
- if (*buffer == '#') {
- send(client, buffer, bufsize, 0);
- *buffer = '*';
- isExit = true;
- }
- } while (*buffer != 42);
- cout << "Server: ";
- do {
- recv(client, buffer, bufsize, 0);
- cout << buffer << " ";
- if (*buffer == '#') {
- *buffer = '\n';
- isExit = true;
- }
- } while (*buffer != 42);
- cout << endl;
- } while (!isExit);
- /* ---------------- CLOSE CALL ------------- */
- /* ----------------- close() --------------- */
- /*
- Once the server presses # to end the connection,
- the loop will break and it will close the server
- socket connection and the client connection.
- */
- cout << "\n=> Connection terminated.\nGoodbye...\n";
- close(client);
- }
- else if(choice == 2)
- {
- printf("TODO");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement