SlideShare a Scribd company logo
Socket Programming
TCP Sockets
UDP Sockets
Socket
 int s = socket(domain, type, protocol);
where
 s: socket descriptor, an integer (like a file-handle)
 domain: integer, communication domain
 e.g.,AF_INET (IPv4 protocol) or AF_UNIX
 type: communication type
 SOCK_STREAM: reliable, 2-way, connection-based service
 SOCK_DGRAM: unreliable, connectionless
 protocol: e.g.,TCP or UDP
 use IPPROTO_TCP or IPPROTO_UDP to send/receiveTCP or UDP packets
Example : s = socket(AF_INET, SOCK_STREAM, 0);
 int s = socket(domain, type, protocol);
where
 s: socket descriptor, an integer (like a file-handle)
 domain: integer, communication domain
 e.g.,AF_INET (IPv4 protocol) or AF_UNIX
 type: communication type
 SOCK_STREAM: reliable, 2-way, connection-based service
 SOCK_DGRAM: unreliable, connectionless
 protocol: e.g.,TCP or UDP
 use IPPROTO_TCP or IPPROTO_UDP to send/receiveTCP or UDP packets
Example : s = socket(AF_INET, SOCK_STREAM, 0);
Bind
 associates an IP address and port for use by the socket
 int status = bind(s, &addrport, size)
 status: return status, 0 if successful, -1 otherwise
 s: socket being used
 addrport: address structure uses sockaddr_in
 size: the size (in bytes) of the addrport structure
 struct sockaddr_in
{
sa_family_t sin_family; /* address family:AF_INET */
u_int16_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
 struct in_addr /* Internet address */
{
u_int32_t s_addr; /* address in network byte order */
};
 associates an IP address and port for use by the socket
 int status = bind(s, &addrport, size)
 status: return status, 0 if successful, -1 otherwise
 s: socket being used
 addrport: address structure uses sockaddr_in
 size: the size (in bytes) of the addrport structure
 struct sockaddr_in
{
sa_family_t sin_family; /* address family:AF_INET */
u_int16_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
 struct in_addr /* Internet address */
{
u_int32_t s_addr; /* address in network byte order */
};
Listen
 The listen system call allows the process to listen on the socket for connections.
 int status = listen(s, queuelen)
 status: return value, 0 if listening, -1 if error
 s: socket being used
 queuelen: number of active participants that can “wait” for a connection
Example : listen(s, 5);
 The listen system call allows the process to listen on the socket for connections.
 int status = listen(s, queuelen)
 status: return value, 0 if listening, -1 if error
 s: socket being used
 queuelen: number of active participants that can “wait” for a connection
Example : listen(s, 5);
accept
 Use the accept function to accept a connection request from a remote host
 The function returns a socket corresponding to the accepted connection
 int ns = accept(sock, &cliaddr, &addrlen)
 ns: new socket used for data-transfer
 sock: original socket being listened on (e.g., server)
 cliaddr: address structure of the active participant (e.g., client)
 The accept function updates/returns the sockaddr structure with the client's address
information
 addrlen: size (in bytes) of the client sockaddr structure
 The accept function updates/returns this value
 Use the accept function to accept a connection request from a remote host
 The function returns a socket corresponding to the accepted connection
 int ns = accept(sock, &cliaddr, &addrlen)
 ns: new socket used for data-transfer
 sock: original socket being listened on (e.g., server)
 cliaddr: address structure of the active participant (e.g., client)
 The accept function updates/returns the sockaddr structure with the client's address
information
 addrlen: size (in bytes) of the client sockaddr structure
 The accept function updates/returns this value
Connect
 The connect function is used by a client program to establish
communication with a remote entity
 int status = connect(sock, &servaddr, addrlen);
 status: return value, 0 if successful connect, -1 otherwise
 sock: client’s socket to be used in connection
 servaddr: server’s address structure
 addrlen: size (in bytes) of the servaddr structure
 The connect function is used by a client program to establish
communication with a remote entity
 int status = connect(sock, &servaddr, addrlen);
 status: return value, 0 if successful connect, -1 otherwise
 sock: client’s socket to be used in connection
 servaddr: server’s address structure
 addrlen: size (in bytes) of the servaddr structure
Sending / Receiving Data
 Send data
 int count = send(int s, const void * msg, int len, unsigned int falgs);
Where:
 count: number of bytes transmitted (-1 if error)
 sock: socket being used
 buf: buffer to be transmitted
 len: length of buffer (in bytes) to transmit
 flags: special options, usually just 0
 Receive data
 int count = recv(int s, void *buf, int len, unsigned int flags);
Where:
 count: number of bytes received (-1 if error)
 sock: socket being used
 buf: stores received bytes
 len: number of bytes received
 flags: special options, usually just 0
 Send data
 int count = send(int s, const void * msg, int len, unsigned int falgs);
Where:
 count: number of bytes transmitted (-1 if error)
 sock: socket being used
 buf: buffer to be transmitted
 len: length of buffer (in bytes) to transmit
 flags: special options, usually just 0
 Receive data
 int count = recv(int s, void *buf, int len, unsigned int flags);
Where:
 count: number of bytes received (-1 if error)
 sock: socket being used
 buf: stores received bytes
 len: number of bytes received
 flags: special options, usually just 0
close
 When finished using a socket, the socket should be closed:
 status = close(s);
 status: return value, 0 if successful, -1 if error
 s: the file descriptor (socket being closed)
TCP echo Server TCP echo Client
#include<stdio.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char**argv) {
int sockfd,newsockfd,clength;
struct sockaddr_in serv_addr,cli_addr;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("nStart");
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("nListening...");
listen(sockfd,5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength);
printf("nAccepted");
read(newsockfd,buffer,4096);
printf("nClient message:%s",buffer);
write(newsockfd,buffer,4096);
close(sockfd);
return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char*argv[]) {
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("nReady for sending...");
connect(sockfd,(struct sockaddr*) &serv_addr, sizeof(serv_addr));
printf("nEnter the message to sendn");
printf("nClient: ");
fgets(buffer,4096,stdin);
write(sockfd,buffer,4096);
printf("Serverecho:%s",buffer);
close(sockfd);
return 0;
}
#include<stdio.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char**argv) {
int sockfd,newsockfd,clength;
struct sockaddr_in serv_addr,cli_addr;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("nStart");
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("nListening...");
listen(sockfd,5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength);
printf("nAccepted");
read(newsockfd,buffer,4096);
printf("nClient message:%s",buffer);
write(newsockfd,buffer,4096);
close(sockfd);
return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char*argv[]) {
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("nReady for sending...");
connect(sockfd,(struct sockaddr*) &serv_addr, sizeof(serv_addr));
printf("nEnter the message to sendn");
printf("nClient: ");
fgets(buffer,4096,stdin);
write(sockfd,buffer,4096);
printf("Serverecho:%s",buffer);
close(sockfd);
return 0;
}
END
END

More Related Content

PPT
Sockets
Gopaiah Sanaka
 
PPT
Sockets intro
AviNash ChaVhan
 
PPT
sockets_intro.ppt
AnilGupta681764
 
PPT
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
senthilnathans25
 
PPT
Basic socket programming
Kristian Arjianto
 
PPT
Socket System Calls
Avinash Varma Kalidindi
 
PPTX
Basics of sockets
AviNash ChaVhan
 
PPT
Application Layer and Socket Programming
elliando dias
 
Sockets
Gopaiah Sanaka
 
Sockets intro
AviNash ChaVhan
 
sockets_intro.ppt
AnilGupta681764
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
senthilnathans25
 
Basic socket programming
Kristian Arjianto
 
Socket System Calls
Avinash Varma Kalidindi
 
Basics of sockets
AviNash ChaVhan
 
Application Layer and Socket Programming
elliando dias
 

Similar to socketProgramming-TCP-and UDP-overview.pdf (20)

PPTX
Lecture 1 Socket programming elementary tcp sockets.pptx
MonaSayed27
 
PPTX
Socket programming in c
Md. Golam Hossain
 
PPTX
Network programming using python
Ali Nezhad
 
PPTX
Socket.io v.0.8.3
Cleveroad
 
PPTX
Socket.io v.0.8.3
Maryna Vasina
 
PPT
Socket Programming
CEC Landran
 
PPTX
Socket programming
Muhammad Fouad Ilyas Siddiqui
 
PPTX
Socket programming
Anurag Tomar
 
PPT
Network Prog.ppt
EloOgardo
 
PPT
Network programming-Network for engineering
insdcn
 
PPT
Npc08
vamsitricks
 
PPT
Net Programming.ppt
EloAcubaOgardo
 
DOC
socket programming
prashantzagade
 
DOC
socket programming
prashantzagade
 
PPTX
Socket Programming
VisualBee.com
 
PPTX
L5-Sockets.pptx
ycelgemici1
 
PDF
Socket programming using C
Ajit Nayak
 
PDF
Network Sockets
Peter R. Egli
 
Lecture 1 Socket programming elementary tcp sockets.pptx
MonaSayed27
 
Socket programming in c
Md. Golam Hossain
 
Network programming using python
Ali Nezhad
 
Socket.io v.0.8.3
Cleveroad
 
Socket.io v.0.8.3
Maryna Vasina
 
Socket Programming
CEC Landran
 
Socket programming
Muhammad Fouad Ilyas Siddiqui
 
Socket programming
Anurag Tomar
 
Network Prog.ppt
EloOgardo
 
Network programming-Network for engineering
insdcn
 
Net Programming.ppt
EloAcubaOgardo
 
socket programming
prashantzagade
 
socket programming
prashantzagade
 
Socket Programming
VisualBee.com
 
L5-Sockets.pptx
ycelgemici1
 
Socket programming using C
Ajit Nayak
 
Network Sockets
Peter R. Egli
 
Ad

More from Shilpachaudhari10 (6)

PPTX
Unit-2-04-digitatoDogitalconversion.pptx
Shilpachaudhari10
 
PDF
Unit-1-03-signal-in data-communication.pdf
Shilpachaudhari10
 
PPT
Lect_Intro-Unit-1-wirelessAdHocNetowrk.ppt
Shilpachaudhari10
 
PPTX
Unit-5-Chapter_7=-8_vWirelessNetworks.pptx
Shilpachaudhari10
 
PPTX
Unit - Chapter_7-and-8-mobile-netwrok-security.pptx
Shilpachaudhari10
 
PDF
chap13-digitalsignature.pdf
Shilpachaudhari10
 
Unit-2-04-digitatoDogitalconversion.pptx
Shilpachaudhari10
 
Unit-1-03-signal-in data-communication.pdf
Shilpachaudhari10
 
Lect_Intro-Unit-1-wirelessAdHocNetowrk.ppt
Shilpachaudhari10
 
Unit-5-Chapter_7=-8_vWirelessNetworks.pptx
Shilpachaudhari10
 
Unit - Chapter_7-and-8-mobile-netwrok-security.pptx
Shilpachaudhari10
 
chap13-digitalsignature.pdf
Shilpachaudhari10
 
Ad

Recently uploaded (20)

DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPT
Ppt for engineering students application on field effect
lakshmi.ec
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
Information Retrieval and Extraction - Module 7
premSankar19
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Ppt for engineering students application on field effect
lakshmi.ec
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 

socketProgramming-TCP-and UDP-overview.pdf

  • 4. Socket  int s = socket(domain, type, protocol); where  s: socket descriptor, an integer (like a file-handle)  domain: integer, communication domain  e.g.,AF_INET (IPv4 protocol) or AF_UNIX  type: communication type  SOCK_STREAM: reliable, 2-way, connection-based service  SOCK_DGRAM: unreliable, connectionless  protocol: e.g.,TCP or UDP  use IPPROTO_TCP or IPPROTO_UDP to send/receiveTCP or UDP packets Example : s = socket(AF_INET, SOCK_STREAM, 0);  int s = socket(domain, type, protocol); where  s: socket descriptor, an integer (like a file-handle)  domain: integer, communication domain  e.g.,AF_INET (IPv4 protocol) or AF_UNIX  type: communication type  SOCK_STREAM: reliable, 2-way, connection-based service  SOCK_DGRAM: unreliable, connectionless  protocol: e.g.,TCP or UDP  use IPPROTO_TCP or IPPROTO_UDP to send/receiveTCP or UDP packets Example : s = socket(AF_INET, SOCK_STREAM, 0);
  • 5. Bind  associates an IP address and port for use by the socket  int status = bind(s, &addrport, size)  status: return status, 0 if successful, -1 otherwise  s: socket being used  addrport: address structure uses sockaddr_in  size: the size (in bytes) of the addrport structure  struct sockaddr_in { sa_family_t sin_family; /* address family:AF_INET */ u_int16_t sin_port; /* port in network byte order */ struct in_addr sin_addr; /* internet address */ };  struct in_addr /* Internet address */ { u_int32_t s_addr; /* address in network byte order */ };  associates an IP address and port for use by the socket  int status = bind(s, &addrport, size)  status: return status, 0 if successful, -1 otherwise  s: socket being used  addrport: address structure uses sockaddr_in  size: the size (in bytes) of the addrport structure  struct sockaddr_in { sa_family_t sin_family; /* address family:AF_INET */ u_int16_t sin_port; /* port in network byte order */ struct in_addr sin_addr; /* internet address */ };  struct in_addr /* Internet address */ { u_int32_t s_addr; /* address in network byte order */ };
  • 6. Listen  The listen system call allows the process to listen on the socket for connections.  int status = listen(s, queuelen)  status: return value, 0 if listening, -1 if error  s: socket being used  queuelen: number of active participants that can “wait” for a connection Example : listen(s, 5);  The listen system call allows the process to listen on the socket for connections.  int status = listen(s, queuelen)  status: return value, 0 if listening, -1 if error  s: socket being used  queuelen: number of active participants that can “wait” for a connection Example : listen(s, 5);
  • 7. accept  Use the accept function to accept a connection request from a remote host  The function returns a socket corresponding to the accepted connection  int ns = accept(sock, &cliaddr, &addrlen)  ns: new socket used for data-transfer  sock: original socket being listened on (e.g., server)  cliaddr: address structure of the active participant (e.g., client)  The accept function updates/returns the sockaddr structure with the client's address information  addrlen: size (in bytes) of the client sockaddr structure  The accept function updates/returns this value  Use the accept function to accept a connection request from a remote host  The function returns a socket corresponding to the accepted connection  int ns = accept(sock, &cliaddr, &addrlen)  ns: new socket used for data-transfer  sock: original socket being listened on (e.g., server)  cliaddr: address structure of the active participant (e.g., client)  The accept function updates/returns the sockaddr structure with the client's address information  addrlen: size (in bytes) of the client sockaddr structure  The accept function updates/returns this value
  • 8. Connect  The connect function is used by a client program to establish communication with a remote entity  int status = connect(sock, &servaddr, addrlen);  status: return value, 0 if successful connect, -1 otherwise  sock: client’s socket to be used in connection  servaddr: server’s address structure  addrlen: size (in bytes) of the servaddr structure  The connect function is used by a client program to establish communication with a remote entity  int status = connect(sock, &servaddr, addrlen);  status: return value, 0 if successful connect, -1 otherwise  sock: client’s socket to be used in connection  servaddr: server’s address structure  addrlen: size (in bytes) of the servaddr structure
  • 9. Sending / Receiving Data  Send data  int count = send(int s, const void * msg, int len, unsigned int falgs); Where:  count: number of bytes transmitted (-1 if error)  sock: socket being used  buf: buffer to be transmitted  len: length of buffer (in bytes) to transmit  flags: special options, usually just 0  Receive data  int count = recv(int s, void *buf, int len, unsigned int flags); Where:  count: number of bytes received (-1 if error)  sock: socket being used  buf: stores received bytes  len: number of bytes received  flags: special options, usually just 0  Send data  int count = send(int s, const void * msg, int len, unsigned int falgs); Where:  count: number of bytes transmitted (-1 if error)  sock: socket being used  buf: buffer to be transmitted  len: length of buffer (in bytes) to transmit  flags: special options, usually just 0  Receive data  int count = recv(int s, void *buf, int len, unsigned int flags); Where:  count: number of bytes received (-1 if error)  sock: socket being used  buf: stores received bytes  len: number of bytes received  flags: special options, usually just 0
  • 10. close  When finished using a socket, the socket should be closed:  status = close(s);  status: return value, 0 if successful, -1 if error  s: the file descriptor (socket being closed)
  • 11. TCP echo Server TCP echo Client #include<stdio.h> #include<netinet/in.h> #include<netdb.h> #define SERV_TCP_PORT 5035 int main(int argc,char**argv) { int sockfd,newsockfd,clength; struct sockaddr_in serv_addr,cli_addr; char buffer[4096]; sockfd=socket(AF_INET,SOCK_STREAM,0); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=INADDR_ANY; serv_addr.sin_port=htons(SERV_TCP_PORT); printf("nStart"); bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); printf("nListening..."); listen(sockfd,5); clength=sizeof(cli_addr); newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength); printf("nAccepted"); read(newsockfd,buffer,4096); printf("nClient message:%s",buffer); write(newsockfd,buffer,4096); close(sockfd); return 0; } #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> #define SERV_TCP_PORT 5035 int main(int argc,char*argv[]) { int sockfd; struct sockaddr_in serv_addr; struct hostent *server; char buffer[4096]; sockfd=socket(AF_INET,SOCK_STREAM,0); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); serv_addr.sin_port=htons(SERV_TCP_PORT); printf("nReady for sending..."); connect(sockfd,(struct sockaddr*) &serv_addr, sizeof(serv_addr)); printf("nEnter the message to sendn"); printf("nClient: "); fgets(buffer,4096,stdin); write(sockfd,buffer,4096); printf("Serverecho:%s",buffer); close(sockfd); return 0; } #include<stdio.h> #include<netinet/in.h> #include<netdb.h> #define SERV_TCP_PORT 5035 int main(int argc,char**argv) { int sockfd,newsockfd,clength; struct sockaddr_in serv_addr,cli_addr; char buffer[4096]; sockfd=socket(AF_INET,SOCK_STREAM,0); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=INADDR_ANY; serv_addr.sin_port=htons(SERV_TCP_PORT); printf("nStart"); bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); printf("nListening..."); listen(sockfd,5); clength=sizeof(cli_addr); newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength); printf("nAccepted"); read(newsockfd,buffer,4096); printf("nClient message:%s",buffer); write(newsockfd,buffer,4096); close(sockfd); return 0; } #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> #define SERV_TCP_PORT 5035 int main(int argc,char*argv[]) { int sockfd; struct sockaddr_in serv_addr; struct hostent *server; char buffer[4096]; sockfd=socket(AF_INET,SOCK_STREAM,0); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); serv_addr.sin_port=htons(SERV_TCP_PORT); printf("nReady for sending..."); connect(sockfd,(struct sockaddr*) &serv_addr, sizeof(serv_addr)); printf("nEnter the message to sendn"); printf("nClient: "); fgets(buffer,4096,stdin); write(sockfd,buffer,4096); printf("Serverecho:%s",buffer); close(sockfd); return 0; }