服务端
//
// 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;
}