C语言socket连接问题!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言socket连接问题!相关的知识,希望对你有一定的参考价值。

[code]#include <windows.h>#include <stdio.h> #include <Winsock2.h>#pragma comment(lib,"ws2_32")#define PORT 1129#define ADDRESS "127.0.0.1/socket.asp"int main() //加载winsock库WSADATA wsaData;WORD sockVersion = MAKEWORD(2,2);if(WSAStartup(sockVersion,&wsaData)!=0) return 0; //创建套接字SOCKET sClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(sClient==INVALID_SOCKET) printf("socket error\n"); return 0; //socket中装入远程地址信息SOCKADDR_IN remote;remote.sin_family = AF_INET;remote.sin_port = htons(1129);remote.sin_addr.S_un.S_addr = inet_addr(ADDRESS); if(connect(sClient,(SOCKADDR *)&remote,sizeof(SOCKADDR))==SOCKET_ERROR) printf("connect error\n"); closesocket(sClient); return 0; while(true) char SendData[255]; int i,ch; for( i = 0; (i < 254) && ((ch = getchar()) != EOF) && (ch!='\n'); i++) SendData[i] = (char)ch; SendData[i] = '\n'; SendData[i+1] = '\0'; send(sClient,SendData,i+2,0); closesocket(sClient);WSACleanup();return 0;[/code]编译通不过,请指正下。

#include <windows.h> #include <stdio.h> #include <winsock.h> #pragma comment(lib,"ws2_32") #define PORT 1129 #define ADDRESS "127.0.0.1/socket.asp" int main() WSADATA wsaData; WORD sockVersion = MAKEWORD(2,2); if(WSAStartup(sockVersion,&wsaData)!=0) return 0; SOCKET sClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(sClient==INVALID_SOCKET) printf("socket error\n"); return 0; sockaddr_in remote; remote.sin_family = AF_INET; remote.sin_port = htons(1129); remote.sin_addr.s_addr = inet_addr(ADDRESS); if(connect(sClient,(SOCKADDR *)&remote,sizeof(SOCKADDR))==SOCKET_ERROR) printf("connect error\n"); closesocket(sClient); return 0; while(true) char SendData[255]; int i,ch; for( i = 0; (i < 254) && ((ch = getchar()) != EOF) && (ch!='\n'); i++) SendData[i] = (char)ch; SendData[i] = '\n'; SendData[i+1] = '\0'; send(sClient,SendData,i+2,0); closesocket(sClient); WSACleanup(); return 0; //帮你修改了下...可编译过...VC6.0 参考技术A 明后天去济南的车票,上午十点或下午两点的有没有

以上是关于C语言socket连接问题!的主要内容,如果未能解决你的问题,请参考以下文章

c语言报文转发脚本怎么写

如何用c语言编写socket套接字

在C/C++/ObjC编程语言中有没有啥方法获取 socket 是不是连接啊?就像C#中Socke

C语言socket编程

C语言关于socket中如何判断客户端是不是与服务器保持连接

C语言 socket listen()函数(socket()函数创建的socket默认是一个主动类型的,listen函数将socket变为被动类型的,等待客户的连接请求)