0% found this document useful (0 votes)
55 views4 pages

Socket Programming for Beginners

The document contains code for a client-server program that allows a client to send a roll number to a server, which responds with the name associated with that roll number. The client code initializes Winsock, creates a socket, connects to the server, sends the roll number, and receives the name in response. The server code initializes Winsock, creates a socket, binds to a port, listens for connections, receives the roll number from the client, gets the corresponding name, and sends the name back to the client.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views4 pages

Socket Programming for Beginners

The document contains code for a client-server program that allows a client to send a roll number to a server, which responds with the name associated with that roll number. The client code initializes Winsock, creates a socket, connects to the server, sends the roll number, and receives the name in response. The server code initializes Winsock, creates a socket, binds to a port, listens for connections, receives the roll number from the client, gets the corresponding name, and sends the name back to the client.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Client

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>

#define PORT 65432


#define BUFFER_SIZE 1024

int main() {
WSADATA wsa;
SOCKET client_socket;
struct sockaddr_in server_addr;
int roll_number;
char buffer[BUFFER_SIZE] = {0};

// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("WSAStartup failed.\n");
return 1;
}

// Create socket
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("Socket creation failed.\n");
return 1;
}

// Set up server address


server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

// Connect to server
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
printf("Connection failed.\n");
return 1;
}

// Get input from user


printf("Enter roll number: ");
scanf("%d", &roll_number);

// Send roll number to server


send(client_socket, (char *)&roll_number, sizeof(roll_number), 0);
// Receive name from server
recv(client_socket, buffer, BUFFER_SIZE, 0);
printf("Name corresponding to roll number %d: %s\n", roll_number, buffer);

// Close the connection


closesocket(client_socket);

// Cleanup
WSACleanup();

return 0;
}

Server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>

#define PORT 65432


#define BUFFER_SIZE 1024

// Function to get name based on roll number


char* get_name(int roll_number) {
// Dummy implementation, replace with your own logic
if (roll_number == 101)
return "Chinmoy";
else if (roll_number == 102)
return "Bob";
else if (roll_number == 103)
return "Charlie";
else
return "Unknown";
}

int main() {
WSADATA wsa;
SOCKET server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
int addr_len = sizeof(client_addr);
int roll_number;
char *name;
char buffer[BUFFER_SIZE] = {0};

// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("WSAStartup failed.\n");
return 1;
}

// Create socket
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("Socket creation failed.\n");
return 1;
}

// Set up server address


server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;

// Bind socket
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) ==
SOCKET_ERROR) {
printf("Bind failed.\n");
return 1;
}

// Listen for incoming connections


if (listen(server_socket, 3) == SOCKET_ERROR) {
printf("Listen failed.\n");
return 1;
}

printf("Server is listening for connections...\n");

// Accept incoming connection


if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &addr_len)) ==
INVALID_SOCKET) {
printf("Accept failed.\n");
return 1;
}

printf("Connected by %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

// Receive roll number from client


recv(client_socket, (char *)&roll_number, sizeof(roll_number), 0);
printf("Received roll number: %d\n", roll_number);

// Get name based on roll number


name = get_name(roll_number);

// Send name to client


send(client_socket, name, strlen(name), 0);
// Close the connection
closesocket(client_socket);
closesocket(server_socket);

printf("Connection closed.\n");

// Cleanup
WSACleanup();

return 0;
}

You might also like