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>
- void error (const char *msg);
- int main (int argc, char *argv[])
- {
- /* 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 buffer[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 csocket76\r\n", strlen("NICK csocket76\r\n"));
- /* send username to the server */
- n = write(sockfd, "USER csocket76 8 * :linux\r\n", strlen("USER csocket76 8 * :linux\r\n"));
- /* join the channel */
- printf("Enter channel to join ex. JOIN #<channel> ");
- fgets(buffer, 255, stdin);
- n = write(sockfd, buffer, strlen(buffer));
- while (buffer != "*quit")
- {
- bzero(buffer, 256);
- n = read(sockfd, buffer, 255);
- /* if the server sends ping we send pong */
- /* and a message to keep our connection alive */
- if (strstr(buffer, "PING :") != NULL) {
- send_message = "PONG :Linux rocks !!";
- write(sockfd, send_message, strlen(send_message));
- printf("%s\n", send_message);
- }
- printf("Type your message ex. PRIVMSG #linux :<message> ");
- /* clear the buffer */
- bzero(buffer,256);
- /* Input data from user */
- fgets(buffer, 255, stdin);
- /* write data to server */
- n = write(sockfd, buffer, strlen(buffer));
- /* print error message if it can't write the data */
- if (n < 0)
- error("ERROR writing to socket");
- }
- /* 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