客户端
/*
client.c
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
const int Port = 12345 ; //端口
char *gIp = "127.0.0.1"; //服务器地址,127.0.0.1 localhost
typedef struct sockaddr SockAddrStruct;
typedef struct sockaddr_in SockAddrStructIn;
int main()
{
int SocketFd = -1;
char Buf[100] = {0};
char Msg[100] = {0};
SockAddrStructIn Addr_in;
Addr_in.sin_family = AF_INET;
Addr_in.sin_port = htons(Port);
Addr_in.sin_addr.s_addr = inet_addr(gIp);
printf("clint start:\n" );
if( ( SocketFd = socket( AF_INET , SOCK_STREAM , 0 ) ) < 0 )
{
perror("socket create failed !\n");
return -1;
}
if( (connect( SocketFd , (SockAddrStruct* )&Addr_in , sizeof( Addr_in) )) < 0 )
{
perror("connect failed !\n");
return -1;
}
printf("client connect\n");
while(1)
{
printf("client input data:");
scanf("%s", Buf);
snprintf(Msg, sizeof(Msg)-1, "client:%s", Buf);
if( (send(SocketFd, Msg, sizeof(Msg), 0)) < 0 )
{
perror("client send failed !\n");
close(SocketFd);
return -1;
}
if( 0 == strcmp(Buf , "quit") )
{
close(SocketFd);
return 0 ;
}
if( ( recv( SocketFd , Msg , sizeof(Msg) , 0 ) ) < 0 )
{
perror("client recv failed !\n");
close(SocketFd);
return -1;
}
printf("%s\n" ,Msg);
}
return 0;
}
服务端
/*
server.c
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
typedef struct sockaddr SockAddrStruct;
typedef struct sockaddr_in SockAddrStructIn;
const char *Ip = "127.0.0.1";
const int Port = 12345;
int main()
{
int SocketFd = -1;
int NewFd = -1 ;
char Msg[100] = {0};
char Buf[100] = {0};
int Len = 0 ;
SockAddrStructIn Addr_in;
memset(&Addr_in, 0 , sizeof(Addr_in));
Addr_in.sin_family = AF_INET;
Addr_in.sin_port = htons(Port);
Addr_in.sin_addr.s_addr = inet_addr(Ip);
if( ( SocketFd = socket( AF_INET , SOCK_STREAM , 0 ) ) < 0 )
{
perror("server socket create failed !\n");
return -1;
}
if( ( bind(SocketFd ,(SockAddrStruct* )&Addr_in , sizeof( Addr_in) ) ) < 0 )
{
perror("bind failed !\n");
return -1;
}
if( ( listen( SocketFd , 100 ) ) < 0 )
{
perror("listen failed !\n");
return -1;
}
printf("server is start\n");
Len = sizeof(Addr_in);
if( ( NewFd = accept(SocketFd,(SockAddrStruct* )&Addr_in,&Len) ) < 0 )
{
perror("accept failed !\n");
return -1 ;
}
close(SocketFd);
while(1)
{
if( ( recv( NewFd , Msg , sizeof(Msg) , 0 ) ) <= 0 )
{
perror("server recv failed !\n");
close( NewFd );
return -1;
}
if( 0 == strcmp(Msg, "quit") )
{
printf("client send the msg is quit\n");
close(NewFd);
return 0 ;
}
snprintf(Buf, sizeof(Buf) - 1, "server:%s", Msg);
if(( write( NewFd, Buf, sizeof(Buf) ) )<0)
{
perror("server send Fd failed !\n");
close( NewFd );
return -1 ;
}
}
close( NewFd );
return 0;
}
本地进程间通信
/*
ipc_by_socket.c
local client-server
*/
#include <stdio.h>
#include <unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid = 0 ;
int status = 0 ;
write(STDOUT_FILENO , "IPC by Socket\n" , sizeof("IPC by Socket\n")-1 );
if( ( pid = fork() ) < 0 ) //服务器
{
perror("fork fail\n");
exit(0);
}
else if( 0 == pid ) //子进程
{
execl( "./server", (char*)0);
perror("execl fail \n");
exit(0);
}
else//父进程
{
sleep(2);
}
if( ( pid = fork() ) < 0 ) //客户端
{
perror("fork fail\n");
exit(0);
}
else if( 0 == pid ) //子进程
{
execl( "./client", (char*)0);
perror("execl fail \n");
exit(0);
}
else//父进程
{
sleep(2);
}
wait(&status);
}