0% found this document useful (0 votes)
5 views9 pages

Lab 01 NP SujanSubedi

The document is a report on socket programming using C, detailing the implementation of a client-server architecture for TCP socket communication. It covers the objectives, server and client code, results of the execution, and concludes with the importance of socket programming in networked applications. The report serves as a practical guide for understanding basic networking concepts and socket APIs.

Uploaded by

shariashrestha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Lab 01 NP SujanSubedi

The document is a report on socket programming using C, detailing the implementation of a client-server architecture for TCP socket communication. It covers the objectives, server and client code, results of the execution, and concludes with the importance of socket programming in networked applications. The report serves as a practical guide for understanding basic networking concepts and socket APIs.

Uploaded by

shariashrestha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

A Report on

Socket Programming Using C

Submitted in the Partial Fulfillment of the


Requirements for the Degree of Bachelor of Software Engineering Awarded by
Pokhara University

Submitted By:
Sujan Subedi
(22180091)

School of Engineering
Faculty of Science and Technology
POKHARA UNIVERSITY

May 2025
Table of Content

Table of Content.........................................................................................................................i
1. Introduction............................................................................................................................1
2. Objectives...............................................................................................................................1
3. Implementation......................................................................................................................2
3.1 Server Code......................................................................................................................2
3.2 Client Code.......................................................................................................................4
4. Result.....................................................................................................................................5
4.1 Server Output...................................................................................................................5
4.2 Client Output....................................................................................................................6
5.Conclusion..............................................................................................................................6

i
1. Introduction

Network programming is a fundamental concept in computer science that allows processes to


communicate over a network. A core part of network programming is socket programming,
which provides a way for programs to communicate using standard networking protocols
such as TCP/IP.

A socket is an endpoint for communication between two machines. Sockets allow for sending
and receiving data between systems either on the same network or over the internet. In the
context of TCP/IP, socket programming enables the creation of client-server architectures,
where the server waits for incoming client requests and the client initiates a connection to the
server.

The two programs in this report illustrate basic TCP socket communication using C on a local
machine (127.0.0.1) with a server listening on port 8080 and a client sending a simple
message to it.

2. Objectives

 To understand the basics of TCP socket programming in C.

 To implement a simple client-server architecture.

 To demonstrate the process of sending and receiving messages over a network socket.

 To learn how different socket APIs (like socket(), bind(), listen(), accept(), connect(),
send(), and read()) work together.

1
3. Implementation

3.1 Server Code

Explanation

server_fd = socket(AF_INET, Creates a TCP socket using IPv4.


SOCK_STREAM, 0);

setsockopt(server_fd, SOL_SOCKET, Allows the server to reuse the address if it was


SO_REUSEADDR, &opt, sizeof(opt)); recently in use.

sockaddress.sin_family = AF_INET; Sets up the address structure: IPv4, any local


IP address, and port 8080.
sockaddress.sin_addr.s_addr =
INADDR_ANY;

2
sockaddress.sin_port = htons(PORT);

bind(server_fd, (struct sockaddr Binds the socket to the IP/port so it can start
*)&sockaddress, sizeof(sockaddress)); listening.

listen(server_fd, 3); Prepares the socket to listen for incoming


connections (backlog of 3).

new_socket = accept(server_fd, (struct Accepts a client connection and returns a


sockaddr *)&sockaddress, (socklen_t new socket descriptor for communication.
*)&addresslen);

val_length = read(new_socket, buffer, Reads the incoming data from the client
1024);

send(new_socket, hello, strlen(hello), 0); Sends a response back to the client

close(new_socket); Closes both the client socket and the server


socket.
shutdown(server_fd, SHUT_RDWR);

3
3.2 Client Code

Explanation

client_fd = socket(AF_INET, Creates a TCP socket using IPv4.


SOCK_STREAM, 0);

serv_addr.sin_family = AF_INET; Sets up the server address (localhost, port


8080)
serv_addr.sin_port = htons(PORT);

inet_pton(AF_INET, "127.0.0.1",
&serv_addr.sin_addr);

connect(client_fd, (struct sockaddr Connects the client to the server


*)&serv_addr, sizeof(serv_addr));

4
send(client_fd, hello, strlen(hello), 0); Sends the message "Hello from client" to
the server.

valread = read(client_fd, buffer, 1024); Receives the server's reply.

4. Result

When the programs are executed (server first, then client), the output is as follows:

4.1 Server Output:

5
4.2 Client Output:

This demonstrates successful bidirectional communication: the client sends a message to the
server, and the server replies.

5. Conclusion

Through this practical implementation of socket programming:

 We learned how to set up a TCP server and client in C.

 We understood the roles of different socket functions.

 We observed how data can be exchanged over a network using sockets.

 The implementation shows the basics of real-time communication over a network and
lays the foundation for more advanced topics such as multi-threaded servers, message
protocols, and security.

Socket programming is crucial for networked applications like web servers, chat
applications, and more. This exercise provided hands-on experience with one of the most
fundamental components of computer networking.

6
7

You might also like