c语言 socket编程

服务端
//
//  main.m
//  server
//
//  Created by mac on 13-4-15.
//  Copyright (c) 2013年 mac. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <sys/types.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>

int CreateServerSocket(short port)
{
    int socket_server=0;
    socket_server=socket(AF_INET,SOCK_STREAM,0);
    //填充IP与端口
    /*
     struct sockaddr_in {
     __uint8_t	sin_len;
     sa_family_t	sin_family;
     in_port_t	sin_port;
     struct	in_addr sin_addr;
     char		sin_zero[8];
     };
     */
    struct sockaddr_in sAddr = {0};
    sAddr.sin_len=sizeof(sAddr);
    sAddr.sin_family=AF_INET;
    sAddr.sin_port= htons(port); //本机字节序转网络字节序
    sAddr.sin_addr.s_addr=INADDR_ANY;
    //绑定  //(struct sockaddr *)&sAddr强转
    if (bind(socket_server, (struct sockaddr *)&sAddr, sizeof(sAddr)) != 0) {
        return 0;
    }
    //监听
    if(listen(socket_server, 100) != 0){
        return 0;
    }
    return socket_server;
}
//接收连接并返回客户端的scoket
int AcceptClientSocket(int ServerSocket)
{
    int ClinetSocket=0;
    struct sockaddr_in cAddr = {0};
    socklen_t len=sizeof(cAddr);
    ClinetSocket = accept(ServerSocket, (struct sockaddr *)&cAddr, &len);
    char * ipaddress=inet_ntoa(cAddr.sin_addr);
    printf("accept--->%s,%d",ipaddress,cAddr.sin_port);
    return ClinetSocket;
}
void Message(int socket)
{
    char send_Message[1024]="hello\n";
    char recv_Message[1024]={0};
    send(socket, send_Message, strlen(send_Message)+1, 0);
    while (1) {
        recv(socket, recv_Message, sizeof(recv_Message), 0);
        printf("<-----%s",recv_Message);
        printf("----->");
        fgets(send_Message, sizeof(send_Message), stdin);
        send(socket,send_Message,strlen(send_Message)+1,0);
    }
}

int main(int argc, const char * argv[])
{
    short port=9898;
    if (argc>1) {
        port=(short)atoi(argv[1]);
    }
    int ServerSocket = CreateServerSocket(port);
    if (ServerSocket == 0) {
        printf("Cerate scoket error\n");
        return 0;
    }
    printf("Cerate scoket ok!\n");
    
    while (true)
    {
        int ClinetScoket=AcceptClientSocket(ServerSocket);
        if (ClinetScoket == 0)
        {
            printf("Client connect error\n");
            return 0;
        }
        printf("Client connect ok.....\n");
        Message(ClinetScoket);
        return 0;
    }
        
//    //接收连接
    accept(<#int#>, <#struct sockaddr *restrict#>, <#socklen_t *restrict#>)
//    struct sockaddr sAdd2={0};
//    socklen_t len=sizeof(struct sockaddr);
//    accept(ServerSocket, &sAdd2, &len);
    
    return 0;
}

客户端:

//
//  main.m
//  clent
//
//  Created by mac on 13-4-15.
//  Copyright (c) 2013年 mac. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <sys/types.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>

int CreateClinetSocket()
{
    int ClinetSocket = 0;
    ClinetSocket = socket(AF_INET, SOCK_STREAM, 0);
    return ClinetSocket;
}
int ConnectionToServer(int client_socket,const char * server_ip,unsigned short port)
{
    struct sockaddr_in sAddr={0};
    sAddr.sin_len=sizeof(sAddr);
    sAddr.sin_family=AF_INET;
    inet_aton(server_ip, &sAddr.sin_addr);
    sAddr.sin_port=htons(port);
    if (connect(client_socket, (struct sockaddr *)&sAddr, sizeof(sAddr))==0) {
        return client_socket;
    }
    return 0;
}
//client
void Message(int socket)
{
    char send_Message[1024]="0";
    char recv_Message[1024]={0};
       while (1) {
        recv(socket, recv_Message, sizeof(recv_Message), 0);
        printf("<-----%s",recv_Message);
        printf("----->");
        fgets(send_Message, sizeof(send_Message), stdin);
        send(socket,send_Message,strlen(send_Message)+1,0);
    }
}
int main(int argc, const char * argv[])
{
    const char * server_ip="127.0.0.1";
    short server_port=9898;
    if (argc>1) {
        server_ip = argv[1];
    }
    if (argc>2) {
        server_port=(short)atoi(argv[2]);
    }
    
    int client_socket= CreateClinetSocket();
    if (ConnectionToServer(client_socket,server_ip, server_port)==0) {
        printf("Connect to server error\n");
        return 0;
    }
    printf("Connect to server ok\n");
    Message(client_socket);
    
    return 0;
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值