// kill -9 $(lsof -ti :8080)
// TCP Server
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main()
{
struct sockaddr_in serverAddr, clientAddr;
int serverAddrLen = sizeof(serverAddr);
int clientAddrLen = sizeof(clientAddr);
// Create a socket
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0)
{
perror("nCouldn't create socket");
}
// Initialize the server address structure
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
// Bind the socket to the server address
if (bind(server_fd, (struct sockaddr *)&serverAddr;, serverAddrLen) < 0)
{
}
// Start listening for incoming connections
if (listen(server_fd, 10) < 0)
{
}
// Accept a client connections
// int comm_fd = accept(server_fd, (struct sockaddr *)&serverAddr;, (socklen_t *)&serverAddrLen;);
int comm_fd = accept(server_fd, (struct sockaddr *)&clientAddr;, &clientAddrLen;);
if (comm_fd < 0)
{
}
char messageReceived[100];
while (1)
{
bzero(messageReceived, 100);
// Receive data from the client
recv(comm_fd, messageReceived, 100, 0);
printf("nMessage from client:n%s", messageReceived
);
// Send the received data back to the client
send
(comm_fd
, messageReceived
, strlen(messageReceived
), 0);
if(strncmp(messageReceived
, "Exit", 4) == 0) {
printf("nServer exiting...n");
break;
}
}
// Close the client connection
close(comm_fd);
close(server_fd);
}
// TCP Client
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main()
{
struct sockaddr_in serverAddr;
// Create a socket
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0)
{
perror("nCouldn't create socket");
}
// Initialize the server address structure
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(PORT);
// if (inet_pton(AF_INET, "127.0.0.1", &serverAddr;.sin_addr) <= 0)
// {
// printf("nInvalid address / Address not supported");
// exit(EXIT_FAILURE);
// }
// Establish a connection to the server
if (connect(client_fd, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) < 0)
{
}
char messageSent[100];
char messageReceived[100];
while (1)
{
bzero(messageSent, 100);
bzero(messageReceived, 100);
// Read a line of text from the user
fgets(messageSent
, 100, stdin
);
// Send the user's input to the server
send(client_fd, messageSent, sizeof(messageSent), 0);
// Receive the response from the server
recv(client_fd, messageReceived, 100, 0);
// Print the server's response
printf("nMessage from server(echo):n%s", messageReceived
);
if (strncmp(messageSent
, "Exit", 4) == 0)
{
printf("nClient exiting...n");
break;
}
}
close(client_fd);
}
// UDP Server
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main()
{
struct sockaddr_in serverAddr, clientAddr;
// Create a socket
int server_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (server_fd < 0)
{
perror("nCouldn't create socket");
}
// Initialize the server address structure
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
// Bind the socket to the server address
if (bind(server_fd, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) < 0)
{
}
char messageReceived[100];
socklen_t clientAddrLen = sizeof(clientAddr);
while (1)
{
bzero(messageReceived, 100);
// Receive data from the client
recvfrom(server_fd, messageReceived, 100, 0, (struct sockaddr *)&clientAddr;, &clientAddrLen;);
printf("nMessage from client:n%s", messageReceived
);
// Send the received data back to the client
sendto
(server_fd
, messageReceived
, strlen(messageReceived
), 0, (struct sockaddr
*)&clientAddr
;, clientAddrLen
);
if(strncmp(messageReceived
, "Exit", 4) == 0) {
printf("nServer exiting...n");
break;
}
}
close(server_fd);
}
// UDP Client
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main()
{
struct sockaddr_in serverAddr;
// Create a socket
int client_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (client_fd < 0)
{
perror("nCouldn't create socket");
}
// Initialize the server address structure
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = INADDR_ANY;
char messageSent[100];
char messageReceived[100];
socklen_t serverAddrLen = sizeof(serverAddr);
while (1)
{
bzero(messageSent, 100);
bzero(messageReceived, 100);
// Read a line of text from the user
fgets(messageSent
, 100, stdin
);
// Send the user's input to the server
sendto(client_fd, messageSent, sizeof(messageSent), 0, (struct sockaddr *)&serverAddr;, serverAddrLen);
// Receive the response from the server
recvfrom(client_fd, messageReceived, 100, 0, (struct sockaddr *)&serverAddr;, &serverAddrLen;);
// Print the server's response
printf("nMessage from server(echo):n%s", messageReceived
);
if(strncmp(messageSent
, "Exit", 4) == 0) {
printf("nClient exiting...n");
break;
}
}
close(client_fd);
}
// Chat Server
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main()
{
struct sockaddr_in serverAddr;
int serverAddrLen = sizeof(serverAddr);
// Create a socket
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0)
{
perror("nCouldn't create socket");
}
// Initialize the server address structure
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
// Bind the socket to the server address
if (bind(server_fd, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) < 0)
{
}
// Start listening for incoming connections
if (listen(server_fd, 10) < 0)
{
}
// Accept a client connections
int comm_fd = accept(server_fd, (struct sockaddr *)&serverAddr;, (socklen_t *)&serverAddrLen;);
if (comm_fd < 0)
{
}
while (1)
{
char messageReceived[100];
char messageSent[100];
// Receive data from the client
recv(comm_fd, messageReceived, 100, 0);
printf("nMessage from client:n%s", messageReceived
);
fgets(messageSent
, 100, stdin
);
// Send the received data back to the client
send
(comm_fd
, messageSent
, strlen(messageSent
), 0);
if(strncmp(messageSent
, "Exit", 4) == 0) {
printf("nServer exiting...n");
break;
}
}
// Close the client connection
close(comm_fd);
close(server_fd);
}
// Chat Client
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main()
{
struct sockaddr_in serverAddr;
// Create a socket
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0)
{
perror("nCouldn't create socket");
}
// Initialize the server address structure
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serverAddr;.sin_addr) <= 0)
{
printf("nInvalid address / Address not supported");
}
// Establish a connection to the server
if (connect(client_fd, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) < 0)
{
}
while (1)
{
char messageSent[100];
char messageReceived[100];
// Read a line of text from the user
fgets(messageSent
, 100, stdin
);
// Send the user's input to the server
send(client_fd, messageSent, sizeof(messageSent), 0);
// Receive the response from the server
recv(client_fd, messageReceived, 100, 0);
// Print the server's response
printf("nMessage from server:n%s", messageReceived
);
if(strncmp(messageReceived
, "Exit", 4) == 0) {
printf("nClient exiting...n");
break;
}
}
close(client_fd);
}