A simple server program that returns uppercase letters

/* myserver.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <ctype.h>

#define SERV_PORT 6666
#define SERV_IP  INADDR_ANY
#define MAXLISTENQ 128

void perr_exit(const char * s)
{
	perror(s);
	exit(1);
}

int main(void)
{
	int listenfd, connfd;
	int n;
	char buf[BUFSIZ] ,clieIP[BUFSIZ];
	struct sockaddr_in servaddr, clieaddr;
	socklen_t clieaddr_len;

	// create socket
	if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) {
		perr_exit("socket error");
	}

	 // bind socket address
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	servaddr.sin_addr.s_addr = htonl(SERV_IP);

	if((n = bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) == -1) {
				perr_exit("bind error");
	}

	 // monitor
	if ((n = listen(listenfd, MAXLISTENQ)) == -1) {
		perr_exit("listen error");
	}

	printf("wait for client connect ...\n");

	clieaddr_len = sizeof(clieaddr);
	if ((connfd = accept(listenfd, (struct sockaddr *)&clieaddr, &clieaddr_len)) == -1) {
		perr_exit("accept error");
	}
	
	 // Display the client's IP address
	if (inet_ntop(AF_INET, &clieaddr.sin_addr.s_addr, clieIP, sizeof(clieIP)) == NULL) {
		perr_exit("inet_ntop error");
	}
	printf("connected---> client IP address: %s\tport: %d\t\n", clieIP, ntohs(clieaddr.sin_port));
	
	while (1) {
		 // Read information from the client
		if ((n = read(connfd, &buf, sizeof(buf))) == -1) {
			perr_exit("read error");
		}
		 // print to standard output
		write(STDOUT_FILENO, buf, n);

		for (int i = 0; i < n; i++) {
			buf[i] = toupper(buf[i]);
		}
		 // Send information to the client
		if ((n = write(connfd, buf, n)) == -1) {
			perr_exit("write to client error");
		}
	}
	return 0;
}


Intelligent Recommendation

Use an interrupt program to convert lowercase letters to uppercase letters

The data to be converted below is to convert conversation to uppercase...

More Recommendation

Uppercase letters to lowercase letters

Uppercase and lowercase letters require the difference between uppercase and lowercase ASCII codes that are familiar with the letters. Uppercase to lowercase is plus 32; otherwise lowercase to upperca...

Javascript implements the function ToLowerCase(), which converts uppercase letters in a string to lowercase letters, and then returns a new string.

The difference of 32 between ASCII values api explanation The charAt() method returns the character at the specified position. "abcd".charAt(2); // "c" The charCodeAt() method retu...

Write a program that will write the English alphabet in a sentence entered by the user to be exchanged (ie uppercase letters to lowercase letters, lowercase letters to uppercase) .C

Draw flowcharts: Created with Raphaël 2.2.0 Start while(a = getchar() != '\n') if (a <= 'Z' and a> = 'A') a = a + 32; printf("%c",a); if (a <= 'z', and a> = 'a') a=a - 32 En...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top