粗糙簡略的初版,後續多加點功能權當練手
/* ============================================================================ Name : server.c Author : king Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdlib.h> #include <pthread.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> /* inet(3) functions */ #include <stdlib.h> #include <errno.h> #include <stdio.h> #include <string.h> int handle(int point); int main(void) { int sfd, ind; struct sockaddr_in addr; struct sockaddr_in clent; char resv[1024], sendbuf[1024]; char buf[1024]; char * myaddr = "192.168.231.128"; int ret; // 返回值設置 socklen_t lent; int pid; addr.sin_family = AF_INET; //IPv4 Internet protocols addr.sin_port = htons(5050); //這裡輸入服務器端口號 addr.sin_addr.s_addr = inet_addr(myaddr); ; //INADDR_ANY表示本機IP //獲取socket描述符,IPV4asd printf("socket start \n"); sfd = socket(AF_INET, SOCK_STREAM, 0); if (sfd < 0) { printf("socket error \n"); return -1; } printf("bind start \n"); //將套接子與指定端口鏈接 if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) { printf("bind error \n"); return -1; } //監聽套接子 printf("listen start \n"); if (listen(sfd, 1024) < 0) { printf("listen error \n"); return -1; } for (;;) { //接受來自客戶端的信息 printf("accept start \n"); memset(&clent, 0, sizeof(clent)); lent = sizeof(clent); ind = accept(sfd, (struct sockaddr *) &clent, &lent); if (ind < 0) { printf("accept error %d \n", ind); return -1; } printf("infor \n"); printf("clent addr%s porit %d\n", inet_ntop(AF_INET, &clent.sin_addr, buf, sizeof(buf)), ntohs(clent.sin_port)); pid = fork(); if (pid == 0) { //子進程 close(sfd); handle(ind); } else if (pid < 0) { //error close(ind); } else { //父進程 } } return EXIT_SUCCESS; } int handle(int point) { int retn; char buf[1024]; for (;;) { retn = read(point, buf, sizeof(buf)); if (retn < 0) { printf("read error \n"); close(point); break; } else if (retn == 0) { printf("client exit \n"); close(point); break; } printf("client:%s\n", buf); if (strcmp("exit", buf) == 0) { printf("exit \n"); close(point); return 0; } } return 0; }
/* ============================================================================ Name : client.c Author : king Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> /* inet(3) functions */ int handle(int fd); int main(void) { int nsd; char buf[1024]; char * myaddr = "192.168.231.128"; struct sockaddr_in addr; printf("welcome to echo client\n"); nsd = socket(AF_INET, SOCK_STREAM, 0); printf("connect start1 \n"); //bzero(addr, sizeof(*addr)); memset(&addr,0,sizeof(addr)); printf("connect start2 \n"); addr.sin_family = AF_INET; addr.sin_port = htons(5050); addr.sin_addr.s_addr=inet_addr(myaddr); printf("connect start3 \n"); if (connect(nsd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0) { printf("connect error \n"); return -1; } sleep(5); printf("handle start\n"); handle(nsd); close(nsd); return EXIT_SUCCESS; } int handle(int fd) { char sendl[1024], rev[1024]; int retn; for (;;) { memset(sendl,0,sizeof(sendl)); memset(rev,0,sizeof(rev)); if (fgets(sendl, 1024, stdin) == NULL) { break; } // printf("wirte start\n"); write(fd, sendl, strlen(sendl)); read(fd, rev,strlen(rev)); } return 0; }
雖然寫程序寫了好幾年,感覺還是技術不過關,於是把socket從新復習了下,
期間遇到錯誤一直沒有編譯過去:
connect和accept
因為沒有深刻了解這兩函數的參數,所以釀成打錯
int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
記住一定是值 addrlen
accept socklen_t *addrlen要是一個指針
這倆個郁悶死我了,我以為是一樣的,結果調了1小時。
還需努力啊。
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#define PORT 5000
int main(int argc, char* argv[])
{
int port=PORT;
WSADATA wsaData;
SOCKET sListen,sAccept;
int iLen;
int iSend;
char buf[]="您好!";
//服務器、客戶的地址
struct sockaddr_in serv,cliet; //聲明兩個地址結構體變量
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
//return;
} //加載套接字庫
//創建一個套接字 AD_INET(IPV4協議) SOCK_STREAM(TCP協議)
sListen=socket(AF_INET,SOCK_STREAM,0);
//判斷是否失敗
if(sListen==INVALID_SOCKET)
{
printf("socket failed:%d\n",WSAGetLastError());
return;
}
為服務器的地址結構體賦值
serv.sin_family=AF_INET; //必須和創建套接字時候一樣(socket)
serv.sin_port=htons(port); //端口 htons轉換為網絡字節序
serv.sin_addr.s_addr=htonl(INADDR_ANY); //綁定的地址 htonl轉換為網絡字節序
//INADDR_ANY 默認的網卡地址 本機
if(bind(sListen,(LPSOCKADDR)&serv,sizeof(serv))==SOCKET_ERROR)
{
printf("bind()failed:%d\n",WSAGetLastError());
return;
}//bind 綁定地址 並且判斷是否成功
if(listen(sListen,5)==SOCKET_ERROR)
{
printf("listen()failed:%d\n",WSAGetLastError());
return;
}//監聽 並且判斷是否成功
iLen=sizeof(cliet); 得到地址結構體的長度 accept函數需要
//循環簡體客戶連接
while(1)
{
sAccept=accept(sListen,(struct sockaddr*)&cliet,&iLen);......余下全文>>
最簡單的就是將dins.readUTF()方法換成dins.readLine(),出錯的原因是到達流的末尾導致的意外終止。
你會發現readLine()方法已經是廢棄方法,不建議使用了。
比較常規的方法是使用
BufferedReader br= new BufferedReader(new InputStreamReader(ins));
來替代DataInputStream
祝你好運!