TCP:
服务器:
#import <Foundation/Foundation.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//
int err;
int fd=socket(AF_INET, SOCK_STREAM , );
BOOL success=(fd!=-);
// 1
// 2
if (success) {
NSLog(@"socket success");
struct sockaddr_in addr;
memset(&addr, , sizeof(addr));
addr.sin_len=sizeof(addr);
addr.sin_family=AF_INET;
// =======================================================================
addr.sin_port=htons();
// ============================================================================
addr.sin_addr.s_addr=INADDR_ANY;
err=bind(fd, (const struct sockaddr *)&addr, sizeof(addr));
success=(err==);
}
// 2
// ============================================================================
if (success) {
NSLog(@"bind(绑定) success");
err=listen(fd, );//开始监听
success=(err==);
}
// ============================================================================
//
if (success) {
NSLog(@"listen success");
while (true) {
struct sockaddr_in peeraddr;
int peerfd;
socklen_t addrLen;
addrLen=sizeof(peeraddr);
NSLog(@"prepare accept");
peerfd=accept(fd, (struct sockaddr *)&peeraddr, &addrLen);
success=(peerfd!=-);
// ============================================================================
if (success) {
NSLog(@"accept success,remote address:%s,port:%d",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
char buf[];
ssize_t count;
size_t len=sizeof(buf);
do {
count=recv(peerfd, buf, len, );
NSString* str = [NSString stringWithCString:buf encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
} while (strcmp(buf, "exit")!=);
}
// ============================================================================
close(peerfd);
}
}
//3
}
return ;
}
客户端:
//
// main.m
// kewai_SocketClient
//
#import <Foundation/Foundation.h>
#include <sys/socket.h>
#include <netinet/in.h>
#import <arpa/inet.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// 1
int err;
int fd=socket(AF_INET, SOCK_STREAM, );
BOOL success=(fd!=-);
struct sockaddr_in addr;
// 1
//
if (success) {
NSLog(@"socket success");
memset(&addr, , sizeof(addr));
addr.sin_len=sizeof(addr);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=INADDR_ANY;
err=bind(fd, (const struct sockaddr *)&addr, sizeof(addr));
success=(err==);
}
// 2
//
if (success) {
//============================================================================
struct sockaddr_in peeraddr;
memset(&peeraddr, , sizeof(peeraddr));
peeraddr.sin_len=sizeof(peeraddr);
peeraddr.sin_family=AF_INET;
peeraddr.sin_port=htons();
// peeraddr.sin_addr.s_addr=INADDR_ANY;
peeraddr.sin_addr.s_addr=inet_addr("172.16.10.120");
// 这个地址是服务器的地址,
socklen_t addrLen;
addrLen =sizeof(peeraddr);
NSLog(@"connecting");
err=connect(fd, (struct sockaddr *)&peeraddr, addrLen);
success=(err==);
if (success) {
// struct sockaddr_in addr;
err =getsockname(fd, (struct sockaddr *)&addr, &addrLen);
success=(err==);
//============================================================================
//============================================================================
if (success) {
NSLog(@"connect success,local address:%s,port:%d",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port));
char buf[];
do {
printf("input message:");
scanf("%s",buf);
send(fd, buf, , );
} while (strcmp(buf, "exit")!=);
}
}
else{
NSLog(@"connect failed");
}
}
// ============================================================================
//
}
return ;
}
UDP:
服务器:
/*
*UDP/IP应用编程接口(API)
*服务器的工作流程:首先调用socket函数创建一个Socket,然后调用bind函数将其与本机
*地址以及一个本地端口号绑定,接收到一个客户端时,服务器显示该客户端的IP地址,并将字串
*返回给客户端。
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#import <arpa/inet.h>
int main(int argc,char **argv)
{
int ser_sockfd;
int len;
//int addrlen;
socklen_t addrlen;
char seraddr[];
struct sockaddr_in ser_addr;
/*建立socket*/
ser_sockfd=socket(AF_INET,SOCK_DGRAM,);
if(ser_sockfd<)
{
printf("I cannot socket success\n");
return ;
}
/*填写sockaddr_in 结构*/
addrlen=sizeof(struct sockaddr_in);
bzero(&ser_addr,addrlen);
ser_addr.sin_family=AF_INET;
ser_addr.sin_addr.s_addr=htonl(INADDR_ANY);
ser_addr.sin_port=htons();
/*绑定客户端*/
if(bind(ser_sockfd,(struct sockaddr *)&ser_addr,addrlen)<)
{
printf("connect");
return ;
}
while()
{
bzero(seraddr,sizeof(seraddr));
len=recvfrom(ser_sockfd,seraddr,sizeof(seraddr),,(struct sockaddr*)&ser_addr,&addrlen);
/*显示client端的网络地址*/
printf("receive from %s\n",inet_ntoa(ser_addr.sin_addr));
/*显示客户端发来的字串*/
printf("recevce:%s",seraddr);
/*将字串返回给client端*/
sendto(ser_sockfd,seraddr,len,,(struct sockaddr*)&ser_addr,addrlen);
}
}
客户端:
/*
*UDP/IP应用编程接口(API)
*客户端的工作流程:首先调用socket函数创建一个Socket,填写服务器地址及端口号,
*从标准输入设备中取得字符串,将字符串传送给服务器端,并接收服务器端返回的字
*符串。最后关闭该socket。
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <netinet/in.h>
#import <arpa/inet.h>
int GetServerAddr(char * addrname)
{
printf("please input server addr:");
scanf("%s",addrname);
return ;
}
int main(int argc,char **argv)
{
int cli_sockfd;
int len;
socklen_t addrlen;
char seraddr[];
struct sockaddr_in cli_addr;
char buffer[];
GetServerAddr(seraddr);
/* 建立socket*/
cli_sockfd=socket(AF_INET,SOCK_DGRAM,);
if(cli_sockfd<)
{
printf("I cannot socket success\n");
return ;
}
/* 填写sockaddr_in*/
addrlen=sizeof(struct sockaddr_in);
bzero(&cli_addr,addrlen);
cli_addr.sin_family=AF_INET;
cli_addr.sin_addr.s_addr=inet_addr(seraddr);
//cli_addr.sin_addr.s_addr=htonl(INADDR_ANY);
cli_addr.sin_port=htons();
bzero(buffer,sizeof(buffer));
/* 从标准输入设备取得字符串*/
len=read(STDIN_FILENO,buffer,sizeof(buffer));
/* 将字符串传送给server端*/
sendto(cli_sockfd,buffer,len,,(struct sockaddr*)&cli_addr,addrlen);
/* 接收server端返回的字符串*/
len=recvfrom(cli_sockfd,buffer,sizeof(buffer),,(struct sockaddr*)&cli_addr,&addrlen);
//printf("receive from %s\n",inet_ntoa(cli_addr.sin_addr));
printf("receive: %s",buffer);
close(cli_sockfd);
}