代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
socket编程
作者:
ny5168
/ 发布于
2013/4/16
/
549
在xcode下的c语言编程
服务端: // // 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 // client // // 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; }
试试其它关键字
socket
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
ny5168
贡献的其它代码
(
1
)
.
socket编程
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3