Upd and TCP With Select
Upd and TCP With Select
h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define PORT 5000
#define MAXLINE 1024
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
int main()
{
int listenfd, connfd, udpfd, nready, maxfdp1;
char buffer[MAXLINE];
pid_t childpid;
fd_set rset;
ssize_t n;
socklen_t len;
const int on = 1;
struct sockaddr_in cliaddr, servaddr;
char* message = "Hello Client";
void sig_chld(int);
// get maxfd
maxfdp1 = max(listenfd, udpfd) + 1;
for (;;) {
int n, len;
// Creating socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("socket creation failed");
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "Hello Server");
write(sockfd, buffer, sizeof(buffer));
printf("Message from server: ");
read(sockfd, buffer, sizeof(buffer));
puts(buffer);
close(sockfd);
}
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h>
#define PORT 5000
#define MAXLINE 1024
int main()
{
int sockfd;
char buffer[MAXLINE];
char* message = "Hello Server";
struct sockaddr_in servaddr;
int n, len;
// Creating socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("socket creation failed");
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));