Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* irc chat program will write to the irc chat server your text */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <sys/time.h>
- void error (const char *msg);
- int main (int argc, char *argv[])
- {
- /* set up the read_fd_set structure */
- fd_set read_fd_set;
- struct timeval tv;
- /* wait 5 seconds for input before leaving fgets */
- tv.tv_sec = 5;
- tv.tv_usec = 0;
- /* Declare variables for the socket, port number and socket value */
- int sockfd, portno, n;
- /* Declare the socket server address structure */
- struct sockaddr_in serv_addr;
- /* Declare server host structure */
- struct hostent *server;
- /* Declare a variable for the text to be read from the server */
- char buffer1[256];
- char buffer2[256];
- /* Declare a character pointer to send a message */
- /* to the server if they ping us */
- char *send_message;
- /* prints error message if the input is invaild */
- if (argc < 3)
- {
- fprintf(stderr, "usage %s hostname port\n", argv[0]);
- exit(0);
- }
- /* converts string to integer and stores the port number */
- portno = atoi(argv[2]);
- /* open the socket */
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- /* prints error message if socket can't be opened */
- if (sockfd < 0)
- error("ERROR opening socket");
- else
- printf("socket opened sucessfully\n");
- /* stores server name to connect to */
- server = gethostbyname(argv[1]);
- /* prints error message if host does not exist */
- if (server == NULL)
- {
- fprintf(stderr, "ERROR, no such host\n");
- exit(0);
- }
- else
- printf("Found host\n");
- /* clears the server address variable of data */
- bzero((char *) &serv_addr, sizeof(serv_addr));
- /* assigns server address to structure */
- serv_addr.sin_family = AF_INET;
- /* copy server address structure to the server host structure */
- bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
- /* Assigns the port number to the port structure */
- serv_addr.sin_port = htons(portno);
- /* connect to the server */
- if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
- error ("ERROR connecting");
- else
- printf("connection sucessful\n");
- /* send nickname to the server */
- n = write(sockfd, "NICK linux2015\r\n", strlen("NICK linux2015\r\n"));
- /* send username to the server */
- n = write(sockfd, "USER linux2015 8 * :linux\r\n", strlen("USER linux2015 8 * :linux\r\n"));
- /* join the channel */
- printf("Enter channel to join ex. JOIN #<channel> ");
- fgets(buffer2, 255, stdin);
- n = write(sockfd, &buffer2, strlen(buffer2));
- printf("press enter to chat\n");
- while (1)
- {
- /* zero out the read_fd_set variable */
- FD_ZERO(&read_fd_set);
- /* setup I/O multiplexing for read and write */
- FD_SET(0, &read_fd_set);
- FD_SET(sockfd, &read_fd_set);
- /* create our select to know how long to wait for input on fgets */
- select(10, &read_fd_set, NULL, NULL, &tv);
- /* if user press enter there is input to be read */
- if (FD_ISSET(0, &read_fd_set))
- {
- printf("Type your message ex. PRIVMSG #linux :<message> ");
- /* clear the buffer */
- bzero(buffer2,256);
- /* get chat text from user */
- fgets(buffer2, 255, stdin);
- /* write data to server */
- n = write(sockfd, &buffer2, strlen(buffer2));
- /* print error message if it can't write the data */
- if (n < 0)
- error("ERROR writing to socket");
- }
- /* if user goes 5 seconds without input leave fgets and read for potential ping message */
- if (FD_ISSET(sockfd, &read_fd_set))
- {
- bzero(buffer1, 256);
- n = read(sockfd, &buffer1, 255);
- /* if the server sends ping we send pong */
- /* and a message to keep our connection alive */
- if (strstr(buffer1, "PING :") != NULL) {
- send_message = "PONG :Linux rocks !!";
- write(sockfd, send_message, strlen(send_message));
- printf("%s\n", send_message);
- }
- }
- }
- /* close socket before exiting */
- close(sockfd);
- return 0;
- }
- void error (const char *msg)
- {
- perror(msg);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment