Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #define MAX 100
- #define SA struct sockaddr
- #include <net/if.h>
- #include <sys/ioctl.h>
- //added for nonblock
- #define BACKLOG 10
- #include <fcntl.h>
- // Returns hostname for the local computer
- void checkHostName(int hostname)
- {
- if (hostname == -1)
- {
- perror("gethostname");
- exit(1);
- }
- }
- // Returns host information corresponding to host name
- void checkHostEntry(struct hostent * hostentry)
- {
- if (hostentry == NULL)
- {
- perror("gethostbyname");
- exit(1);
- }
- }
- // Converts space-delimited IPv4 addresses
- // to dotted-decimal format
- void checkIPbuffer(char *IPbuffer)
- {
- if (NULL == IPbuffer)
- {
- perror("inet_ntoa");
- exit(1);
- }
- }
- // Function for server side chat
- void servfunc(int sockfd, char *ip, int cliport)
- {
- char buff[100];
- int n;
- bzero(buff, 100);
- read(sockfd, buff, sizeof(buff));
- printf("Message recieve from %s\n", ip);
- printf("Sender's Port: %d\n", cliport);
- printf("Message: %s\n", buff);
- //chat back and forth with client
- /*
- char buff[100];
- int n;
- // infinite loop for chat
- for (;;) {
- bzero(buff, MAX);
- // read the message from client and copy it in buffer
- read(sockfd, buff, sizeof(buff));
- // print buffer which contains the client contents
- printf("Type 'exit' to exit chat\n");
- printf("Message recieve from %s\n", ip);
- printf("Sender's Port: %d\n", cliport);
- printf("Message: %s\n", buff);
- printf("Message: ");
- bzero(buff, MAX);
- n = 0;
- // copy server message in the buffer
- while ((buff[n++] = getchar()) != '\n')
- ;
- // and send that buffer to client
- write(sockfd, buff, sizeof(buff));
- // if msg contains "Exit" then server exit and chat ended.
- if (strncmp("close", buff, 4) == 0) {
- printf("Server Exit...\n");
- break;
- }
- }
- */
- }
- //Function for client side chat
- void clifunc(int clifd, int portnum)
- {
- //chat back and forth with server
- /*
- char buff[100];
- int n;
- for (;;) {
- bzero(buff, sizeof(buff));
- printf("Type 'exit' to exit chat\n");
- printf("Message : ");
- n = 0;
- while ((buff[n++] = getchar()) != '\n')
- ;
- write(clifd, buff, sizeof(buff));
- printf("Message sent to port no: %d\n", portnum);
- bzero(buff, sizeof(buff));
- read(clifd, buff, sizeof(buff));
- printf("From Port %d: %s\n", portnum, buff);
- if ((strncmp(buff, "exit", 4)) == 0) {
- printf("Client Exit...\n");
- break;
- }
- }
- */
- //write not working
- char buff2[100];
- int o = 0;
- bzero(buff2, sizeof(buff2));
- printf("Enter the message: ");
- while((buff2[o++] = getchar()) != '\n')
- ;
- write(clifd, buff2, sizeof(buff2));
- bzero(buff2, sizeof(buff2));
- printf("Message sent to port: %d\n", portnum);
- /*
- //one message
- //send cant take spaces
- char buff2[100];
- int o;
- printf("Enter the message : ");
- char msgtoserver[MAX];
- scanf("%s",msgtoserver);
- send(clifd, msgtoserver, strlen(msgtoserver), 0);
- bzero(buff2, sizeof(buff2));
- */
- }
- //for unblock
- int guard(int n, char* err){ if ( n == -1) { perror(err); exit(1); } return n;}
- int main(int argc, char *argv[]){
- int sockfd, connfd, len;
- struct sockaddr_in servaddr, cli;
- int portnum;
- int choice;
- int done = 0;
- //for myip
- char hostbuffer[256];
- char *IPbuffer;
- struct hostent *host_entry;
- int hostname;
- int connectyorn = 0;
- int inputport;
- char *destination;
- int ready = 0;
- int portnumber;
- //int myport;
- unsigned int myport;
- //for send clientside
- struct sockaddr_in cliaddr, cliaddr2;
- int clifd;
- if(argc < 2){
- sockfd = guard(socket(AF_INET, SOCK_STREAM, 0), "could not create");
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- int flags = guard(fcntl(sockfd, F_GETFL), "could not get flags");
- guard(fcntl(sockfd, F_SETFL, flags | O_NONBLOCK), "could not set to nonblocking");
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons(0);
- guard(bind(sockfd, (SA*)&servaddr, sizeof(servaddr)), "could not bind");
- int length = sizeof(servaddr);
- getsockname(sockfd, (SA*)&servaddr, &length);
- myport = ntohs(servaddr.sin_port);
- printf("My port: %u \n", myport);
- guard(listen(sockfd, 100), "could not listen");
- while(done != 9999){
- //added for unblock socket
- socklen_t addr_size;
- addr_size = sizeof(struct sockaddr_in);
- int clientfd = accept(sockfd, (SA*)&cli,&addr_size);
- if(clientfd == -1){
- if(errno == EWOULDBLOCK){
- printf("No pending connections.\n");
- sleep(1);
- }
- /*
- else{
- printf("error accepting connection");
- //exit(1);
- }
- */
- }
- else{
- //for single msg recieve
- /*
- char buff[MAX];
- bzero(buff, MAX);
- // read the message from client and copy it in buffer
- read(clientfd, buff, sizeof(buff));
- //to get ip
- char ip[INET_ADDRSTRLEN];
- inet_ntop(AF_INET, &(cli.sin_addr),ip , INET_ADDRSTRLEN);
- //printf("Message recieve from: %s\n", inet_ntoa(servaddr.sin_addr));
- printf("Message recieve from: %s\n", ip);
- printf("Sender's Port: %d\n", ntohs(cli.sin_port));
- printf("Message: %s\n", buff);
- */
- char *ip = inet_ntoa(cli.sin_addr);
- int cliport = ntohs(cli.sin_port);
- servfunc(clientfd, ip, cliport);
- close(sockfd);
- }
- printf("1.help\n");
- printf("2.myip\n");
- printf("3.myport\n");
- printf("4.connect <destination> <port no>\n");
- printf("5.list\n");
- printf("6.terminate <connection id.>\n");
- printf("7.send <connection id.> <message>\n");
- printf("8.exit\n");
- printf("9.Refresh\n");
- scanf("%d", &choice);
- switch(choice){
- case 1: printf("2. myip: Display IP address of the process.\n");
- printf("3. myport: Display the port on which the process is listening for incoming connections.\n");
- printf("4. connect: Connect to specified <destination> at the specified <port no>\n");
- printf("5. list: Display a numbered list of all the connections the process is part of.\n");
- printf("6. terminate: Terminate the connection listed under the specified number when LIST is used to display all connections.\n");
- printf("7. send: Send message to the host of the connect that is designated by the number 3 when command 'list' is used.\n");
- printf("8. exit: Close all connections and terminate the process.\n");
- break;
- case 2:
- // To retrieve hostname
- hostname = gethostname(hostbuffer, sizeof(hostbuffer));
- checkHostName(hostname);
- // To retrieve host information
- host_entry = gethostbyname(hostbuffer);
- checkHostEntry(host_entry);
- // To convert an Internet network
- // address into ASCII string
- IPbuffer = inet_ntoa(*((struct in_addr*)
- host_entry->h_addr_list[0]));
- checkIPbuffer(IPbuffer);
- //printf("Hostname: %s\n", hostbuffer);
- printf("Host IP: %s\n", IPbuffer);
- break;
- case 3:
- printf("My port: %u \n", myport);
- break;
- case 4:
- printf("connect <destination> <port no>\n");
- // printf("Input destination: ");
- //char *destination;
- // scanf("%s", destination);
- printf("Input port no: ");
- scanf("%d", &inputport);
- portnum = inputport;
- // socket create and varification
- clifd = socket(AF_INET, SOCK_STREAM, 0);
- if (clifd == -1) {
- printf("socket creation failed...\n");
- exit(0);
- }
- else
- printf("Socket successfully created..\n");
- bzero(&servaddr, sizeof(servaddr));
- // assign IP, PORT
- cliaddr.sin_family = AF_INET;
- //127.0.0.1
- // servaddr.sin_addr.s_addr = inet_addr(destination);
- cliaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- cliaddr.sin_port = htons(portnum);
- //added
- cliaddr2.sin_family = AF_INET;
- cliaddr2.sin_addr.s_addr = inet_addr("127.0.0.1");
- cliaddr2.sin_port = htons(myport);
- //cant bind to myport to get correct port number on clifunc
- if(bind(clifd, (SA*)&cliaddr2, sizeof(struct sockaddr_in)) == 0 ){
- printf("Binded\n");
- }
- else{
- printf("Unable to Bind\n");
- }
- // connect the client socket to server socket
- if (connect(clifd, (SA*)&cliaddr, sizeof(cliaddr)) != 0) {
- printf("connection with the server failed...\n");
- //exit(0);
- }
- else{
- printf("connected to the server..\n");
- connectyorn = 1;
- ready = 1;
- }
- break;
- case 5: printf("list");
- break;
- case 6: printf("terminate");
- break;
- case 7:
- if(connectyorn == 1){
- // function for chat
- clifunc(clifd, portnum);
- // close the socket
- close(clifd);
- }
- else
- printf("Not connected to server\n");
- break;
- case 8: printf("Closing program\n");
- done = 9999;
- break;
- case 9:
- break;
- }
- }
- }
- else{
- portnum = atoi(argv[1]);
- struct sockaddr_in cliaddr, cliaddr2;
- int clifd;
- // socket create and varification
- clifd = socket(AF_INET, SOCK_STREAM, 0);
- if (clifd == -1) {
- printf("socket creation failed...\n");
- exit(0);
- }
- else
- printf("Socket successfully created..\n");
- //bzero(&servaddr, sizeof(servaddr));
- //assign port and ip
- cliaddr.sin_family = AF_INET;
- cliaddr.sin_addr.s_addr = INADDR_ANY;
- cliaddr.sin_port = htons(portnum);
- cliaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- //explicity assign port by binding client with port
- cliaddr2.sin_family = AF_INET;
- cliaddr2.sin_addr.s_addr = INADDR_ANY;
- cliaddr2.sin_port = htons(0);
- cliaddr2.sin_addr.s_addr = inet_addr("127.0.0.1");
- if(bind(clifd, (SA*)&cliaddr2, sizeof(struct sockaddr_in)) == 0 ){
- printf("Binded\n");
- }
- else{
- printf("Unable to Bind\n");
- }
- int length = sizeof(cliaddr2);
- getsockname(clifd, (SA*)&cliaddr2, &length);
- myport = ntohs(cliaddr2.sin_port);
- printf("My port: %u \n", myport);
- // connect the client socket to server socket
- if (connect(clifd, (SA*)&cliaddr, sizeof(cliaddr)) != 0) {
- printf("connection with the server failed...\n");
- exit(0);
- }
- else{
- printf("connected to the server..\n");
- // function for chat
- clifunc(clifd, portnum);
- // close the socket
- close(clifd);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement