send bitmap file using windows socket

hi all,I want to send a bitmap file that was witten in VC++ by using socket programming.Firstly I can send message form client to server.But how can I send the file.The code is below Thanks

Server part:

#include <iostream>

#include <winsock.h>

#include <windows.h>

#include <vector>

using namespace std;

#pragma comment(lib, "wsock32.lib")

void socketError(char*);

char * readline(SOCKET *s);

int main()

{

WORD sockVersion;

WSADATA wsaData;

int rVal;

sockVersion = MAKEWORD(1,1);

//start dll

WSAStartup(sockVersion, &wsaData);

//create socket

SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

if(s ==-1)

{

cout<<"eror creating socket\n";

WSACleanup();

return(0);

}

//fill in sockaddr_in struct

SOCKADDR_IN sin;

sin.sin_family = PF_INET;

sin.sin_port = htons(8888);

sin.sin_addr.s_addr = INADDR_ANY;

//bind the socket

rVal = bind(s, (LPSOCKADDR)&sin, sizeof(sin));

if(rVal ==-1)

{

cout<< "bind failed\n";

WSACleanup();

return(0);

}

//get socket to listen

rVal = listen(s, 2);

if(rVal ==-1)

{

cout<<"listen failed\n";

WSACleanup();

return(0);

}

//wait for a client

SOCKET client;

cout << "waiting for newclient" << endl;

client = accept(s, NULL, NULL);

cout << "newclient found" << endl;

if(client == INVALID_SOCKET)

{

cout<<"Failed accept()";

WSACleanup();

return (0);

}

readline(&client);

//close process

closesocket(client);

closesocket(s);

WSACleanup();

cout << "closing down"<< endl;

return 0;

};

char * readline(SOCKET *client)

{

vector<char> theVector;

char buffer;

int rVal;

while(true)

{

rVal = recv(*(client), &buffer, 1, 0);

if(rVal ==-1)

{

int errorVal = WSAGetLastError();

if(errorVal == WSAENOTCONN)

{

cout<<"Socket not connected!";

}

cout<<"Failed recv()";

WSACleanup();

}

if(buffer == '\n')

{

char *data = new char[theVector.size() + 1];

memset(data, 0, theVector.size()+1);

for(int i=0; i<theVector.size(); i+=1)

{

data = theVector;

}

cout << data << endl;

return data;

}

else

{

theVector.push_back(buffer);

}

}

}

void socketError(char* str)

{

MessageBox(NULL, str, "SERVER SOCKET ERROR", MB_OK);

};

Client part

#include <windows.h>

#include <winsock.h>

#include <iostream>

using namespace std;

#pragma comment(lib, "wsock32.lib")

#define CS_ERROR 1

#define CS_OK 0

void sError(char*);

int main()

{

WORD version;

WSADATA wsaData;

int rVal=0;

version = MAKEWORD(1,1);

WSAStartup(version,(LPWSADATA)&wsaData);

LPHOSTENT hostEntry;

//store information about the server

hostEntry = gethostbyname("sami-5rfuhk8in4.");

if(!hostEntry)

{

cout<<"Failed gethostbyname()";

//WSACleanup();

return(0);

}

//create the socket

SOCKET theSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

if(theSocket ==-1)

{

cout<<"Failed socket()";

return (0);

}

//Fill in the sockaddr_in struct

SOCKADDR_IN serverInfo;

serverInfo.sin_family = PF_INET;

serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);

serverInfo.sin_port = htons(8888);

rVal=connect(theSocket,(LPSOCKADDR)&serverInfo, sizeof(serverInfo));

if(rVal==-1)

{

cout<<"Failed connect()";

return(0);

}

char *buf = "simpleservermessage\n";

rVal = send(theSocket, buf, strlen(buf), 0);

if(rVal ==-1)

{

cout<<"Failed send()";

return(0);

}

closesocket(theSocket);

cout << "closing client"<< endl;

WSACleanup();

return CS_OK;

}

void sError(char *str)

{

MessageBox(NULL, str, "SOCKET ERROR", MB_OK);

WSACleanup();

}

[27470 byte] By [sam_bodrum] at [2008-2-22]
# 1

High level steps:

1) create a TCP connection to the server

2) create an array of X bytes long (example x = 1024) -- this is equivalent to the char *buf line in the client example above

3) read X bytes from the file into the array

4) send the contents of the array on the socket

5) repeat steps 3 and 4 until the entire file has been read and sent

6) close socket

MikeFlasko at 2007-9-5 > top of Msdn Tech,Windows Networking Development,Winsock Kernel (WSK)...