/* 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;
}
The data to be converted below is to convert conversation to uppercase...
The results of the operation are as follows...
Open the English text file: The code is as follows: The results are as follows:...
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...
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...
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...