Berikut adalah langkah-langkah socket programming dalam bahasa C beserta capture screennya:
- buka teks editor dan copy paste source code dibawa ini untuk membuat server, simpan dengan nama server.c
/*
C socket server example
*/
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}
//sumber: http://www.binarytides.com/server-client-example-c-sockets-linux/
1: /*
2: C ECHO client example using sockets
3: */
4: #include<stdio.h> //printf
5: #include<string.h> //strlen
6: #include<sys/socket.h> //socket
7: #include<arpa/inet.h> //inet_addr
8: int main(int argc , char *argv[])
9: {
10: int sock;
11: struct sockaddr_in server;
12: char message[1000] , server_reply[2000];
13: //Create socket
14: sock = socket(AF_INET , SOCK_STREAM , 0);
15: if (sock == -1)
16: {
17: printf("Could not create socket");
18: }
19: puts("Socket created");
20: server.sin_addr.s_addr = inet_addr("127.0.0.1");
21: server.sin_family = AF_INET;
22: server.sin_port = htons( 8888 );
23: //Connect to remote server
24: if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
25: {
26: perror("connect failed. Error");
27: return 1;
28: }
29: puts("Connected\n");
30: //keep communicating with server
31: while(1)
32: {
33: printf("Enter message : ");
34: scanf("%s" , message);
35: //Send some data
36: if( send(sock , message , strlen(message) , 0) < 0)
37: {
38: puts("Send failed");
39: return 1;
40: }
41: //Receive a reply from the server
42: if( recv(sock , server_reply , 2000 , 0) < 0)
43: {
44: puts("recv failed");
45: break;
46: }
47: puts("Server reply :");
48: puts(server_reply);
49: }
50: close(sock);
51: return 0;
52: }
53: //sumber: http://www.binarytides.com/server-client-example-c-sockets-linux/
sumber source code:
http://www.binarytides.com/server-client-example-c-sockets-linux/
No comments:
Post a Comment