Lab 01 NP SujanSubedi
Lab 01 NP SujanSubedi
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
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 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
Explanation
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.
val_length = read(new_socket, buffer, Reads the incoming data from the client
1024);
3
3.2 Client Code
Explanation
inet_pton(AF_INET, "127.0.0.1",
&serv_addr.sin_addr);
4
send(client_fd, hello, strlen(hello), 0); Sends the message "Hello from client" to
the server.
4. Result
When the programs are executed (server first, then client), the output is as follows:
5
4.2 Client Output:
This demonstrates successful bidirectional communication: the client sends a message to the
server, and the server replies.
5. Conclusion
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